• 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;
    }
  • 相关阅读:
    测试一面(宇宙条)
    java实现快速排序
    java实现冒泡排序
    Perl 获取当前系统时间
    日常问题解决:记一次因watchdog未启动导致的resin启动失败解决
    日常问题解决:记一次因信号量不足引起的APACHE启动错误解决以及kernel.sem值优化
    oracle11g使用expdp、impdp导出导入用户表结构
    日常问题解决:rhel6解决curl版本过旧问题
    日常问题解决:解决fork: retry: 资源暂时不可用
    日常问题解决:rhel7修改TCP最大连接数
  • 原文地址:https://www.cnblogs.com/null00/p/3101508.html
Copyright © 2020-2023  润新知