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;
    }
  • 相关阅读:
    猜数小游戏
    Please change caller according to com.intellij.openapi.project.IndexNotReadyException documentation。
    Android Studio —— Executing tasks
    C语言如何输出ASCII码
    Generator
    poj1919--Red and Black (DFS)
    poj1699--Best Sequence (DFS+查表)
    poj1753-Flip Game BFS+位运算
    Zombie 僵尸感染--BFS
    Java视频
  • 原文地址:https://www.cnblogs.com/null00/p/3101508.html
Copyright © 2011-2022 走看看