欢迎使用CSDN-markdown编辑器
来源:互联网 发布:藏剑正太捏脸数据 编辑:程序博客网 时间:2024/05/18 02:23
C++ primer 第九章笔记
section 9.1 Overview of Sequential Container
- 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.
- 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
- iterator range: [begin, end)
- begin, cbegin(const), rbegin(reverse)
- XX.begin() is overloaded as const and non-const, depends on whether the members of objects are const or not.
- initialize a container using copy demands that container type and element type are identical
- array
<int, 41> arr
; - 6 ways to initialize:
- C c1;
- C c1(c2); C c1 = c2;
- C c1{1, 2, 3}; C c1 = {1, 2, 3};
- C c1(begin, end);
- C c1(n)
- C c1(n, c)
section 9.3 Sequential Container Operations
seq.assign(i1)
不能使用左侧的iterators,references,pointers- swap 保证O(1)时间复杂度,不会无效左侧的iterators,references,pointers。string除外,array除外(改变内容)
emplace
calls constructor
c.emplace_back("978-0590353403", 25, 15.99);c.push_back(Sales_data("978-0590353403", 25, 15.99));
- c.back(), c.front(), c.at(n), c[n] 都返回reference.
auto &v = c.back(); - calling front or back when empty and c[n], n >= c.size() will cause programming error. at is safe
- pop_back and pop_front return void, so get things done before deleting.
- resize(n), value initialized.(resize(n,t))
- reserve(n), allocate n elements in memory
- shrink_to_fit(), a request to return exceeded memory.
section 9.5 Additonal string Operations
- Additonal ways to construct a str
- string s(cp, n) a copy of n characters from a const char*(null terminated)
- string s(s2, pos2), a copy of the characters starting from position pos2(undefined if s2.size() < pos2)
- 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
- stack, queue, priority_queue.
- adaptors can not use operations of underlying container type
- 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)
- queue: q.front() and q.back(); priority_queue: q.top()
- 注意: s.end()不能再加任何数,不然会报错!!!
0 0
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- cout标准输出流常用控制格式
- 避免僵尸进程的三种方法
- 线程与进程的区别和联系
- HDU 5780 gcd
- 对于tomcat不可以通过IP或者127.0.0.1访问,而只可以通过localhost访问的解决办法
- 欢迎使用CSDN-markdown编辑器
- clipToPadding和android:clipChildren
- Hibernate (开放源代码的对象关系映射框架)
- AtomicInteger的用法
- Struts学习总结
- Recently Viewed Products 加图片
- android studio 计算器textview text右下方显示
- 后缀数组学习笔记
- 学习记录3:树的直径(树的最长链)(poj 1985 Cow Marathon)