<<C++ Primer>>第九章读书笔记


  1. 顺序容器类型
    顺序容器 
    vector支持快速随机访问
    list支持快速插入/删除
    deque双端队列
    顺序容器适配器
    stack后进先出(LIFO)堆栈
    queue先进先出(FIFO)队列
    priority_queue有优先级管理的队列

  2. C++ 语言中,大多数类型都可用作容器的元素类型。容器元素类型必须满足以下两个约束:
    • 元素类型必须支持赋值运算。
    • 元素类型的对象必须可以复制。
    此外,关联容器的键类型还需满足其他的约束,我们将在第十章介绍相关内容。
    大多数类型满足上述最低限度的元素类型要求。除了引用类型外,所有内置或复合类型都可用做元素类型。引用不支持一般意义的赋值运算,因此没有元素是引用类型的容器。
    除输入输出(IO)标准库类型(以及第 17.1.9 节介绍的 auto_ptr 类型)之外,所有其他标准库类型都是有效的容器元素类型。特别地,容器本身也满足上述要求,因此,可以定义元素本身就是容器类型的容器。Sales_item 类型也满足上述要求。IO 库类型不支持复制或赋值。因此,不能创建存放 IO 类型对象的容器。
  3. 注意,在指定容器元素为容器类型时,必须如下使用空格:
    vector< vector<string> > lines; // ok: space required between close >
    vector< vector<string>> lines; // error: >> treated as shift operator
    必须用空格隔开两个相邻的 > 符号,以示这是两个分开的符号,否则,系统会认为 >> 是单个符号,为右移操作符,并导致编译时错误。

  4. 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 是容器类型名,如 vectorT 是元素类型,如 intstring 适用于所有容器。

    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 的副本 ccc2 必须具有相同的容器类型,并存放相同类型的元素。适用于所有容器。

    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,其元素是迭代器 be 标示的范围内元素的副本。适用于所有容器。

    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.

    只适用于顺序容器


  5. 为了使程序更清晰、简短,容器类型最常用的构造函数是默认构造函数。在大多数的程序中,使用默认构造函数能达到最佳运行时性能,并且使容器更容易使用。
  6. 将一个容器复制给另一个容器时,类型必须匹配:容器类型和元素类型都必须相同
  7. 使用迭代器时,不要求容器类型相同。容器内的元素类型也可以不相同,只要它们相互兼容,能够将要复制的元素转换为所构建的新容器的元素类型,即可实现复制。

  8. 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 != iter2

    Compare 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.

    比较两个迭代器是否相等(或不等)。当两个迭代器指向同一个容器中的同一个元素,或者当它们都指向同一个容器的超出末端的下一位置时,两个迭代器相等



  9. Table 9.4. Operations Supported by vector and deque Iterators
    表 9.4. vectordeque 类型迭代器支持的操作

    iter + n
    iter - n

    Adding (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 -= iter2

    Compound-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 容器


  10. 迭代器范围:此类元素范围称为左闭合区间(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 标记的元素前面的元素。
  11. 对形成迭代器范围的迭代器的要求
    迭代器 first 和 last 如果满足以下条件,则可形成一个迭代器范围:
    它们指向同一个容器中的元素或超出末端的下一位置。
    如果这两个迭代器不相等,则对 first 反复做自增运算必须能够到达 last。换句话说,在容器中,last 绝对不能位于 first 之前。
    编译器自己不能保证上述要求。编译器无法知道迭代器所关联的是哪个容器,也不知道容器内有多少个元素。若不能满足上述要求,将导致运行时未定义的行为。
  12. 容器定义的类型别名

    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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值