zoukankan      html  css  js  c++  java
  • C++ Primer 读书笔记 第九章

    1. Two constraints that element types must meet:

        - The element type must support assignment.

        - We must be able to copy objects of the element type.

        So references, IO library types, auto_ptr type are not permitted.

    2. Only vector and deque support iterator arithmetic and the use of relational operators (in addition to == and !=). So list only has it++, but does not have (it + n).

    3. When we add an element to a container, we do so by copying the element value into the container.

    4. Operations to access elements in a sequential container.

    c.back()    returns a reference to the last element c
    c.front()   returns a reference to the first element c
    c[n]        returns a reference to the element in c
                  valid only for vector and deque
    c.at(n)     returns a reference to the element index by n
                  valid only for vector and deque

    5. swap does not invalidate iterators. After swap, iterators continue to refer to the same elements, although those elements are now in a different container.

    6. Inserting elements at the front or back of a deque does not invalidate any iterators. Inserting or erasing anywhere else in the deque invalidates all iterators referring to elements of the deque.

    7. For the string search operations. If there is no match, string::npos is returned.

    8. By default both stack and queue are implemented in terms of deque, and a priority_queue is implemented on a vector.

        A stack can be built on a vector, list, or deque. The queue adapator requires push_front in tis underlying container, and so could be built on a list but not on a vector. A priority_queue requires random access and so can be built on a vector or a deque but not on a list.

    #include <vector>
    #include <iostream>
    using namespace std;
    
    void swap(int &x, int &y)
    {
        int tmp = x;
        x = y;
        y = tmp; 
    }
    
    int main()
    {
        vector<string> vec;
        vec.push_back("Hello");
        vec.push_back("World");
        vec.push_back("ABC");
        vec.push_back("EDF");
        vector<string>::iterator it = vec.begin() + vec.size()/2;
        cout << *it++ << endl;
        cout << *it << endl;
        int x = 2;
        int y = 3;
        swap(x, y);
        cout << "x = " << x << "\ty = " << y << endl;
        string s1 = "hiyaabcdefghi";
        string s2(s1, 2);
        cout << s2 << endl;
        return 0;
    }
  • 相关阅读:
    10. 正则表达式匹配
    124. 二叉树中的最大路径和。 递归
    1028. 从先序遍历还原二叉树。 深搜
    1014. 最佳观光组合. 转变思路
    297. 二叉树的序列化与反序列化.
    1300. 转变数组后最接近目标值的数组和. 二分
    15. 三数之和. 双指针法⭐
    1. 两数之和. 哈希表
    739. Daily Temperatures. 单调栈
    面试题46. 把数字翻译成字符串. 递归
  • 原文地址:https://www.cnblogs.com/null00/p/3101508.html
Copyright © 2011-2022 走看看