欢迎使用CSDN-markdown编辑器

来源:互联网 发布:藏剑正太捏脸数据 编辑:程序博客网 时间:2024/05/18 02:23

C++ primer 第九章笔记

section 9.1 Overview of Sequential Container

  1. Sequential Containter Types:
    • vector: flexible size, fast random access, slow insertion and deletion other than at back
    • deque:double-ended queue, fast random access, fast insert/delete at back and front
    • list:doubly linked list, only bidirectional sequential access, fast delete/insert at any point
    • forward_list:singly linked list, only sequential access, fast delete/insert at any point
    • array:fixed size, fast random access, can’t add or remove elements
    • string: specialized container, similar to vector that contains characters.
  2. Which sequential container to use: rule of thumb
    • use vector unless you have a reason to use others
    • use list or forward_list if program has lots of small elements and space doesn’t matter.
    • insert/delete in the middle: list/forward_list
    • insert/delete at front and back: deque
    • note that some insertion in the middle can use insert and sort instead

section 9.2 Overview of Sequential Container

  1. iterator range: [begin, end)
  2. begin, cbegin(const), rbegin(reverse)
  3. XX.begin() is overloaded as const and non-const, depends on whether the members of objects are const or not.
  4. initialize a container using copy demands that container type and element type are identical
  5. array <int, 41> arr;
  6. 6 ways to initialize:
  7. C c1;
  8. C c1(c2); C c1 = c2;
  9. C c1{1, 2, 3}; C c1 = {1, 2, 3};
  10. C c1(begin, end);
  11. C c1(n)
  12. C c1(n, c)

section 9.3 Sequential Container Operations

  1. seq.assign(i1)不能使用左侧的iterators,references,pointers
  2. swap 保证O(1)时间复杂度,不会无效左侧的iterators,references,pointers。string除外,array除外(改变内容)
  3. emplacecalls constructor
c.emplace_back("978-0590353403", 25, 15.99);c.push_back(Sales_data("978-0590353403", 25, 15.99));
  1. c.back(), c.front(), c.at(n), c[n] 都返回reference.

    auto &v = c.back();
  2. calling front or back when empty and c[n], n >= c.size() will cause programming error. at is safe
  3. pop_back and pop_front return void, so get things done before deleting.
  4. resize(n), value initialized.(resize(n,t))
  5. reserve(n), allocate n elements in memory
  6. shrink_to_fit(), a request to return exceeded memory.

section 9.5 Additonal string Operations

  1. Additonal ways to construct a str
  2. string s(cp, n) a copy of n characters from a const char*(null terminated)
  3. string s(s2, pos2), a copy of the characters starting from position pos2(undefined if s2.size() < pos2)
  4. string s(s2, pos, len2), a copy of len2 characters starting from position pos2. copies at most s2.size() - pos2 characters

section 9.6 Container Adaptors

  1. stack, queue, priority_queue.
  2. adaptors can not use operations of underlying container type
  3. stack can use vector, list, deque(by default). queue can use list and deque(default). priority_queue can use vector(default) and deque(not list because of random access)
  4. queue: q.front() and q.back(); priority_queue: q.top()
  5. 注意: s.end()不能再加任何数,不然会报错!!!
0 0