• Vector使用


    Construct Vector


    vector的初始化函数有6种,分别是 default、fill、range、copy、move、initializer list。

    1 vector<int> fill_(5, 100);
    2 vector<int> copy_(fill_);
    3 vector<int> range_(fill_.begin(), fill_.end());
    4 // move貌似是右值引用,没举例子
    5 int myints[] = {1, 2, 3, 4};
    6 vector<int> init_list(myints, myints + sizeof(myints) / sizeof(int));

    member function


    1. assign

    赋值函数有三种形式:range、fill、initialzer list。

    vector<int> firsrt;
    vector<int> second;
    vector<int> third;
    
    first.assign(4, 100);
    second.assign(first.begin(), first.end());
    
    int myints[] = {1, 2, 3};
    third.assign(myints, myints + sizeof(myints) / sizeof(int));

    2. at

    返回位置的引用,越界会报错。

    vector<int> first(4, 100);
    first.at(2) = 200;

    3. back

    返回最后一个元素的引用,最后一个为空时报错。

    1 vector<int> first(4, 100);
    2 first.back() = 200;

    4. begin && end

    返回的是随机迭代器。

    for(vector<int>::iterator i = v.begin(); i != v.end(); ++i) {}

    5. capacity && max_size && size

    前者返回容量,中间返回理论最大值,后者返回目前大小。

    vector<int> first;
    for (int i=0; i<100; ++i) first.push_back(i);
    cout << first.size() << endl;        //100
    cout << first.capacity() << endl;   //141
    cout << first.max_size() << endl; //1073741823

    6. cbegin && cend(C++11)

    返回的是 const_iterator。

    for(auto i = first.cbegin(); i != first.cend(); ++i) {}

    7. clear

    删除容器中所有的元素,容器容量重置为0.因为使用了swap函数,所以reallocation不保证发生,容量也不保证为0.

    first.clear();

    8. rbegin && rend

    for(auto i = first.rbegin(); i != rend(); ++i) {};

    crbegin和crend就不介绍了。

    9. data(C++11)

    直接返回指定元素的指针。

    int *p = first.data();
    cout << *(++p) << endl;

    10. emplace(C++11)

    构造并且插入给定参数的元素,返回一个迭代器。

    1 // 最后只在第一个成功插入43,其他的没有
    2 first.emplace(first.begin(), 200, 1, 2, 3, 43);

    11. emplace_back(C++11)

    构造并插入给定参数在尾部,返回void。

    first.emplace_back (100, 100);

    12. empty

    返回是否为空。

    13. earse

    删除单个元素或者是[first, last)范围内的元素。返回最后删除的元素迭代器。

    first.earse(first.begin());
    first.earse(first.begin(), first.begin()+5);

    可以配合remove删除所有重复元素:

    first.earse(
        remove(first.begin(), first.end(), 6),
        first.end());

    14. get_allocator()

    返回构造器。

    int *p;
    p = first.get_allocator().allocate(5);

    15. insert()

    插入分5种,single element、fill、range、move、initializer list。

    first.insert(first.begin(), 300);
    first.insert(first.begin(), 2, 300);
    first.insert(first.begin(), second.begin(), second.end());
    int myints = {1, 2, 3}
    first.inert(first.begin(), myints, myints+3);

    16. operator= && operator[]

    一个拷贝函数,一个取值。

    17. pop_back && push_back

    一个弹出,一个插入,注意两个都是返回空的。

    18. reserve && shrink_to_fit(C++11)

    前者设置capacity,否则缩减空间自适应。返回空。

    19. resize

    改变size,还可以设置第二个参数默认数值。

    myvector.resize(5);
    myvector.resize(8,100);
    myvector.resize(12);

    参考


    1. http://www.cplusplus.com/

  • 相关阅读:
    命令[34]
    命令[33]
    命令[27]
    命令[38]
    命令[19]
    命令[22]
    命令[30]
    命令[37]
    命令[23]
    命令[26]
  • 原文地址:https://www.cnblogs.com/mangoyuan/p/6498909.html
Copyright © 2020-2023  润新知