美文网首页
STL Containers

STL Containers

作者: 龙遁流 | 来源:发表于2017-02-04 15:16 被阅读0次

    容器的共性

    1,所有容器中元素都是值而不是引用,元素插入容器的时候容器拷贝或移动元素,即元素必须能够被拷贝或移动。但也可以存储元素的指针来避免拷贝。

    2,容器中的元素都是有特定顺序的,不管是不是有序或无序容器,只要没有进行插入和删除操作,利用迭代器遍历多次得到的顺序是一样的。

    3,STL本身一般不抛出异常

    Req表示容器通用的操作

    移动迭代器

    std::vector<std::string> c(std::make_move_iterator(l.begin()),std::make_move_iterator(l.end()));

    获取数组迭代器

    int arr;

    std::begin(arr),std::end(arr)

    流迭代器

    std::deque<int> c((std::istream_iterator<int>(std::cin)),(std::istream_iterator<int>()));

    All containers except vectors and deques guarantee that iterators and references to elements remain valid if other elements are deleted. For vectors, only the elements before the point of erase remain valid.

    If you remove all elements by using clear(), for vectors, deques, and strings any past-the-end iterator returned by end() or cend() may become invalid.

    If you insert elements, only lists, forward lists, and associative containers guarantee that iterators and references to elements remain valid. For vectors, this guarantee is given if insertions don’t exceed the capacity. For unordered containers, that guarantee is given to references in general but to iterators only when no rehashing happens, which is guaranteed as long as with insertions the number of resulting elements is less than the bucket count times the maximum load factor.

    容器通用类型

    Arrays

    类array<>定义在<array>,为普通的静态数组提供容器的操作,未提供参数为初始值列表的构造函数和赋值操作,但提供了移动操作

    std::array<int, 5>  coll = { 42, 377, 611, 21, 44 };

    array的元素是连续存储的

    std::array<char, 41>a; // create static array of 41 chars

    strcpy(a.data(),"hello, world"); // copy a C-string into the array

    printf("%s\n", a.data()); // print contents of the array as C-string

    array提供tuple接口

    typedef std::array<std::string, 5>  FiveStrings;

    FiveStrings a = { "hello", "nico", "how", "are", "you" };

    std::tuple_size<FiveStrings>::value // yields 5

    std::tuple_element<1,FiveStrings>::type // yields std::string

    std::get<1>(a) // yields std::string("nico")

    // negate all elements

    transform(a.begin(),a.end(), a.begin(),negate<int>()); // operation

    Vectors

    reserve 和 shrink_to_fit使元素的引用,指针,迭代器无效

    vec.data()返回数据的开始指针(STL实现vector是连续的存储空间)

    copy (sentence.cbegin(), sentence.cend(),ostream_iterator(cout," "));//打印

    vector<bool> 中的元素一般为1bit

    Deques

    Lists

    Forward Lists

    不提供size()方法,可用distance算法计算

    可唯一直接访问的元素是第一个

    不提供后向迭代器,不可使用需要双向迭代器的算法,比如sort(),但提供成员函数sort()替代

    Sets and Multisets

    一般使用平衡二叉树(红黑树,改变元素和搜索元素的性能较好)

    可以定义排序准则作为模板参数或构造参数,否则使用默认的排序准则(<)

    sets不允许重复元素,当插入一个已存在元素时会失败,同时返回此时的状态

    Maps and Multimaps

    元素是键值对,按照键的一定顺序将元素排序,multimaps允许重复的键;<map>

    注意:键和值必须支持拷贝或移动操作,键必须支持某种排序规则;

    内部元素组织结构类似于set和multiset

    传递排序规则可以使用模板参数的形式和构造函数参数形式

    map<string, int>::value_type类型表示此map元素的类型

    使用value_type和pair<>及make_pair()构造map的元素

    at()函数返回元素的值得引用或者out_of_range异常如果没有对应的元素;[]操作符如果没有此元素则会插入新的,可能会无意中插入新的值

    Unordered Containers

    <unordered_set>和<unordered_map>

    实现是基于hash表,每个bucket里的元素用链表组织

    元素组织结构

    具有常数时间的插入,删除,搜索性能(但是发生rehash时是现行复杂度)

    影响hash性能的操作,load_factor = 元素个数/桶的个数,表征容器的满的程度

    实现引用语义的方法

    1,shared_ptr<>,容器的元素是指针

    2,std::reference_wapper<>,容器存储的是元素的引用

    相关文章

      网友评论

          本文标题:STL Containers

          本文链接:https://www.haomeiwen.com/subject/jagqittx.html