Effective C++ (8) 顺序容器vector,list,deque

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介:

为了定义一个容器类型的对象,必须先包含相关的头文件,即下列头文件之一:

     #include <vector>
     #include <list>
     #include <deque>

 

所有的容器都是类模板要定义某种特殊的容器,必须在容器名后加一对尖括号,尖括号里面提供容器中存放的元素的类型:

     vector<string>    svec;       // empty vector that can hold strings
     list<int>         ilist;      // empty list that can hold ints
     deque<Sales_item> items;      // empty deque that holds Sales_items
将一个容器初始化为另一个容器的副本

当不使用默认构造函数,而是用其他构造函数初始化顺序容器时,必须指出该容器有多少个元素,并提供这些元素的初值。同时指定元素个数和初值的一个方法是将新创建的容器初始化为一个同类型的已存在容器的副本:

     vector<int> ivec;
     vector<int> ivec2(ivec);   // ok: ivec is vector<int>
     list<int>   ilist(ivec);   // error: ivec is not list<int>
     vector<double> dvec(ivec); // error: ivec holds int not double

 
 

迭代器标记了要复制的元素范围,这些元素用于初始化新容器的元素。迭代器标记出要复制的第一个元素和最后一个元素。采用这种初始化形式可复制不能直接复制的容器。更重要的是,可以实现复制其他容器的一个子序列:

     // initialize slist with copy of each element of svec
     list<string> slist(svec.begin(), svec.end());
     // find midpoint in the vector
     vector<string>::iterator mid = svec.begin() + svec.size()/2;
     // initialize front with first half of svec: The elements up to but not including *mid
     deque<string> front(svec.begin(), mid);
     // initialize back with second half of svec: The elements *mid through end of svec
     deque<string> back(mid, svec.end());

回顾一下指针,我们知道指针就是迭代器,因此允许通过使用内置数组中的一对指针初始化容器也就不奇怪了:

     char *words[] = {"stately", "plump", "buck", "mulligan"};
     // calculate how many elements in words
     size_t words_size = sizeof(words)/sizeof(char *);
     // use entire array to initialize words2
     list<string> words2(words, words + words_size);
分配和初始化指定数目的元素
(1)创建顺序容器时,可显式指定容器大小和一个(可选的)元素初始化式。容器大小可以是常量或非常量表达式,元素初始
化则必须是可用于初始化其元素类型的对象的值:
     const list<int>::size_type list_size = 64;
     list<string> slist(list_size, "eh?"); // 64 strings, each is eh?
(2)也可以只指定大小。
 

支持复制和赋值功能是容器元素类型的最低要求。此外,一些容器操作对元素类型还有特殊要求。如果元素类型不支持这些特殊要求,则相关的容器操作就不能执行:我们可以定义该类型的容器,但不能使用某些特定的操作。

本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2009/02/26/1586315.html,如需转载请自行联系原作者


相关文章
|
1天前
|
存储 缓存 编译器
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
|
1天前
|
算法 C++ 容器
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
【C++进阶(四)】STL大法--list深度剖析&list迭代器问题探讨
|
3天前
|
存储 C++ 容器
c++的学习之路:14、list(1)
c++的学习之路:14、list(1)
16 0
|
9天前
|
索引 容器
06-python数据容器-list列表定义/list的10个常用操作/列表的遍历/使用列表取出偶数
06-python数据容器-list列表定义/list的10个常用操作/列表的遍历/使用列表取出偶数
|
18天前
|
存储 算法 编译器
【C++初阶】11. list的使用及模拟实现
【C++初阶】11. list的使用及模拟实现
46 3
|
29天前
|
存储 算法 Linux
深入理解Linux内存管理brk 和 sbrk 与以及使用C++ list实现内存分配器
深入理解Linux内存管理brk 和 sbrk 与以及使用C++ list实现内存分配器
35 0
|
1月前
|
存储 C++ 容器
C++:List的使用和模拟实现
C++:List的使用和模拟实现
|
1月前
|
存储 算法 测试技术
【C++】容器篇(二)——List的基本概述以及模拟实现
【C++】容器篇(二)——List的基本概述以及模拟实现
|
1月前
|
存储 C++ 容器
C++初阶--list
C++初阶--list
|
1月前
|
安全 Java API
Java并发 - J.U.C并发容器类 list、set、queue
Queue API 阻塞是通过 condition 来实现的,可参考 Java 并发 - Lock 接口 ArrayBlockingQueue 阻塞 LinkedBlockingQueue 阻塞 ArrayQueue 非阻塞 LinkedQueue 非阻塞