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;
    }
  • 相关阅读:
    grafana里prometheus查询语法
    Linux 高频工具快速教程
    国内开源镜像站点汇总
    Oracle DBLINK 简单使用
    启动OpenOffice服务
    使用openoffice转pdf,详细
    ORACLE数据库误操作执行了DELETE,该如何恢复数据?
    一个 介绍 superset Kylin 以及大数据生态圈的 博文
    Kylin介绍 (很有用)
    找到一些经验,关于使用thymeleaf时遇到的一些问题
  • 原文地址:https://www.cnblogs.com/null00/p/3101508.html
Copyright © 2011-2022 走看看