-
顺序容器类型 顺序容器 vector 支持快速随机访问 list 支持快速插入/删除 deque 双端队列 顺序容器适配器 stack 后进先出(LIFO)堆栈 queue 先进先出(FIFO)队列 priority_queue 有优先级管理的队列 - C++ 语言中,大多数类型都可用作容器的元素类型。容器元素类型必须满足以下两个约束:
- 元素类型必须支持赋值运算。
- 元素类型的对象必须可以复制。
大多数类型满足上述最低限度的元素类型要求。除了引用类型外,所有内置或复合类型都可用做元素类型。引用不支持一般意义的赋值运算,因此没有元素是引用类型的容器。
除输入输出(IO)标准库类型(以及第 17.1.9 节介绍的 auto_ptr 类型)之外,所有其他标准库类型都是有效的容器元素类型。特别地,容器本身也满足上述要求,因此,可以定义元素本身就是容器类型的容器。Sales_item 类型也满足上述要求。IO 库类型不支持复制或赋值。因此,不能创建存放 IO 类型对象的容器。 - 注意,在指定容器元素为容器类型时,必须如下使用空格:
必须用空格隔开两个相邻的 > 符号,以示这是两个分开的符号,否则,系统会认为 >> 是单个符号,为右移操作符,并导致编译时错误。vector< vector<string> > lines; // ok: space required between close > vector< vector<string>> lines; // error: >> treated as shift operator
-
Table 9.2. Container Constructors
表 9.2. 容器构造函数
C<T> c;
Create an empty container named c. C is a container name, such as vector, and T is the element type, such as int or string. Valid for all containers.
创建一个名为 c 的空容器。C 是容器类型名,如 vector,T 是元素类型,如 int 或 string 适用于所有容器。
C c(c2);
Create c as a copy of container c2; c and c2 must be the same container type and hold values of the same type. Valid for all containers.
创建容器 c2 的副本 c;c 和 c2 必须具有相同的容器类型,并存放相同类型的元素。适用于所有容器。
C c(b, e);
Create c with a copy of the elements from the range denoted by iterators b and e. Valid for all containers.
创建 c,其元素是迭代器 b 和 e 标示的范围内元素的副本。适用于所有容器。
C c(n, t);
Create c with n elements, each with value t, which must be a value of the element type of C or a type convertible to that type.
用 n 个值为 t 的元素创建容器 c,其中值 t 必须是容器类型 C 的元素类型的值,或者是可转换为该类型的值。
Sequential containers only.
只适用于顺序容器
C c(n);
Create c with n value-initialized (Section 3.3.1, p. 92) elements.
创建有 n 个值初始化(第 3.3.1 节)(value-initialized)元素的容器 c。
Sequential containers only.
只适用于顺序容器
- 为了使程序更清晰、简短,容器类型最常用的构造函数是默认构造函数。在大多数的程序中,使用默认构造函数能达到最佳运行时性能,并且使容器更容易使用。
- 将一个容器复制给另一个容器时,类型必须匹配:容器类型和元素类型都必须相同
- 使用迭代器时,不要求容器类型相同。容器内的元素类型也可以不相同,只要它们相互兼容,能够将要复制的元素转换为所构建的新容器的元素类型,即可实现复制。
-
Table 9.3. Common Iterator Operations
表 9.3. 常用迭代器运算
*iter
Return a reference to the element referred to by the iterator iter.
返回迭代器 iter 所指向的元素的引用
iter->mem
Dereference iter and fetch the member named mem from the underlying element. Equivalent to (*iter).mem.
对 iter 进行解引用,获取指定元素中名为 mem 的成员。等效于 (*iter).mem
++iter iter++
Increment iter to refer to the next element in the container.
给 iter 加 1,使其指向容器里的下一个元素
--iter iter--
Decrement iter to refer to the previous element in the container.
给 iter 减 1,使其指向容器里的前一个元素
iter1 == iter2
iter1 != iter2Compare two iterators for equality (inequality). Two iterators are equal if they refer to the same element of the same container or if they are the off-the-end iterator (Section 3.4, p. 97) for the same container.
比较两个迭代器是否相等(或不等)。当两个迭代器指向同一个容器中的同一个元素,或者当它们都指向同一个容器的超出末端的下一位置时,两个迭代器相等
-
Table 9.4. Operations Supported by vector and deque Iterators
表 9.4. vector 和 deque 类型迭代器支持的操作
iter + n
iter - nAdding (subtracting) an integral value n to (from) an iterator yields an iterator that many elements forward (backward) within the container. The resulting iterator must refer to an element in the container or one past the end of the container.
在迭代器上加(减)整数值 n,将产生指向容器中前面(后面)第 n 个元素的迭代器。新计算出来的迭代器必须指向容器中的元素或超出容器末端的下一位置
iter1 += iter2
iter1 -= iter2Compound-assignment versions of iterator addition and subtraction. Assigns the value of adding or subtracting iter1 and iter2 into iter1.
这里迭代器加减法的复合赋值运算:将 iter1 加上或减去 iter2 的运算结果赋给 iter1
iter1 - iter2
Subtracting two iterators yields the number that when added to the right-hand iterator yields the left-hand iterator. The iterators must refer to elements in the same container or one past the end of the container.
两个迭代器的减法,其运算结果加上右边的迭代器即得左边的迭代器。这两个迭代器必须指向同一个容器中的元素或超出容器末端的下一位置
Supported only for vector and deque.
只适用于 vector 和 deque 容器
>, >=, <, <=
Relational operators on iterators. One iterator is less than another if it refers to an element whose position in the container is ahead of the one referred to by the other iterator. The iterators must refer to elements in the same container or one past the end of the container.
迭代器的关系操作符。当一个迭代器指向的元素在容器中位于另一个迭代器指向的元素之前,则前一个迭代器小于后一个迭代器。关系操作符的两个迭代器必须指向同一个容器中的元素或超出容器末端的下一位置
Supported only for vector and deque.
只适用于 vector 和 deque 容器
- 迭代器范围:此类元素范围称为左闭合区间(left-inclusive interval),其标准表示方式为:
// to be read as: includes first and each element up to but not including last [ first, last )
表示范围从 first 开始,到 last 结束,但不包括 last。迭代器 last 可以等于 first,或者指向 first 标记的元素后面的某个元素,但绝对不能指向 first 标记的元素前面的元素。 - 对形成迭代器范围的迭代器的要求
迭代器 first 和 last 如果满足以下条件,则可形成一个迭代器范围:
它们指向同一个容器中的元素或超出末端的下一位置。
如果这两个迭代器不相等,则对 first 反复做自增运算必须能够到达 last。换句话说,在容器中,last 绝对不能位于 first 之前。
编译器自己不能保证上述要求。编译器无法知道迭代器所关联的是哪个容器,也不知道容器内有多少个元素。若不能满足上述要求,将导致运行时未定义的行为。 - 容器定义的类型别名
size_type
Unsigned integral type large enough to hold size of largest possible container of this container type
无符号整型,足以存储此容器类型的最大可能容器长度
iterator
Type of the iterator for this container type
此容器类型的迭代器类型
const_iterator
Type of the iterator that can read but not write the elements
元素的只读迭代器类型
reverse_iterator
Iterator that addresses elements in reverse order
按逆序寻址元素的迭代器
const_reverse_iterator
Reverse iterator that can read but not write the elements
元素的只读(不能写)逆序迭代器
difference_type
Signed integral type large enough to hold the difference, which might be negative, between two iterators
足够存储两个迭代器差值的有符号整型,可为负数
value_type
Element type
元素类型
reference
Element's lvalue type; synonym for value_type&
元素的左值类型,是 value_type& 的同义词
const_reference
Element's const lvalue type; same as const value_type&
元素的常量左值类型,等效于 const value_type&
转载于:https://www.cnblogs.com/cppfans/articles/2038180.html