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;
    }
  • 相关阅读:
    关于 flutter_boost splash加载动画
    [BS-23] AFN网络请求上拉/下拉刷新的细节问题总结
    [BS-22] Objective-C中nil、Nil、NULL、NSNull的区别
    [BS-21] 关于OC中对象与指针的思考
    [BS-20] 导航控制器和视图控制器在添加控制器的区别
    ios runtime swizzle
    关于C、OC、C++、OC++、Swift的一些常识
    iOS多线程知识总结--GCD
    使用代码创建AutoLayout约束
    iOS中NSLog输出格式大全
  • 原文地址:https://www.cnblogs.com/null00/p/3101508.html
Copyright © 2011-2022 走看看