C++ STL Vector

一、简介

1. Vectors 包含着一系列连续存储的元素,其行为和数组类似。

2. 访问Vector中的任意元素或从末尾添加元素都可以在O(1)内完成,而查找特定值的元素所处的位置或是在Vector中插入元素则是O(N)。

3. 函数介绍

二、例子

1. vector函数int类型测试

#include <vector> 
#include <iostream> 
#include <algorithm> 
#include <stdexcept> 
using namespace std; 
  
void print(int num) 
{ 
    cout << num << " "; 
} 
  
int main() 
{ 
    //1. 初始化 
    vector<int> v; 
    vector<int>::iterator iv; 

    v.reserve(100); //设置vector最小的元素容纳数量 
    v.assign(10, 2); //将10个值为2的元素赋到vector中 
    cout << v.capacity() << endl; //返回vector所能容纳的元素数量(在不重新分配内存的情况下) 
    cout << v.size() << endl; //返回Vector实际含有的元素数量 
    cout << "-------------1--------------" << endl;

    //2. 添加 
    //注意:push_front()只适用于list和deque容器类型 
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }
    for_each(v.begin(), v.end(), print);//需要#include <algorithm> 
    cout << endl; 
    cout << v.size() << endl; 
    cout << "-------------2--------------" << endl; 

    //3. 插入及遍历、逆遍历 
    v.insert(v.begin() + 3, 99); 
    v.insert(v.end() - 3, 88); 
    for_each(v.begin(), v.end(), print); 
    cout << endl; 
    for_each(v.rbegin(), v.rend(), print);//在逆序迭代器上做++运算将指向容器中的前一个元素 
    cout << endl; 
    cout << "-------------3--------------" << endl; 

    //一般遍历写法 
    for(iv = v.begin(); iv != v.end(); ++iv) 
    cout << *iv << " "; 
    cout << endl; 
    cout << "-------------4--------------" << endl;  

    //4. 删除 
    v.erase(v.begin() + 3); 
    for_each(v.begin(), v.end(), print); 
    cout << endl; 
    v.insert(v.begin() + 3, 99);//还原 

    v.erase(v.begin(), v.begin() + 3); //注意删除了3个元素而不是4个 
    for_each(v.begin(), v.end(), print); 
    cout << endl; 
    cout << "-------------5--------------" << endl; 

    //注意:pop_front()只适用于list和deque容器类型 
    v.pop_back(); //删除最后一个元素
    for_each(v.begin(), v.end(), print); 
    cout << endl; 
    cout << "-------------6--------------" << endl; 

    //5. 查询 
    cout << v.front() << endl; 
    cout << v.back() << endl; 
    cout << "-------------7--------------" << endl; 

    //危险的做法,但一般我们就像访问数组那样操作就行,错误的,打印出来的有多出的元素
    for (int i = 15; i < 25; i++) 
        cout << "Element " << i << " is " << v[i] << endl; 
    cout << "-------------8--------------" << endl; 
    //安全的做法 
    int i; 
    try
    { 
        for (i = 15; i < 25; i++) {
            cout << "Element " << i << " is " << v.at(i) << endl; 
        }
    } catch (out_of_range err) { //#include <stdexcept> 
        cout << "out_of_range at " << i << endl; 
    } 
    cout << endl; 
    cout << "-------------9--------------" << endl;

    //6. 清空 
    v.clear(); 
    cout << v.size() << endl;//0 
    for_each(v.begin(), v.end(), print); //已经clear,v.begin()==v.end(),不会有任何结果。 
    cout << "-------------10--------------" << endl;

    return 0; 
}

/*
//执行结果
# ./pp
100
10
-------------1--------------
2 2 2 2 2 2 2 2 2 2 0 1 2 3 4 5 6 7 8 9 
20
-------------2--------------
2 2 2 99 2 2 2 2 2 2 2 0 1 2 3 4 5 6 88 7 8 9 
9 8 7 88 6 5 4 3 2 1 0 2 2 2 2 2 2 2 99 2 2 2 
-------------3--------------
2 2 2 99 2 2 2 2 2 2 2 0 1 2 3 4 5 6 88 7 8 9 
-------------4--------------
2 2 2 2 2 2 2 2 2 2 0 1 2 3 4 5 6 88 7 8 9 
99 2 2 2 2 2 2 2 0 1 2 3 4 5 6 88 7 8 9 
-------------5--------------
99 2 2 2 2 2 2 2 0 1 2 3 4 5 6 88 7 8 
-------------6--------------
99
8
-------------7--------------
Element 15 is 88
Element 16 is 7
Element 17 is 8
Element 18 is 9
Element 19 is 7
Element 20 is 8
Element 21 is 9
Element 22 is 0
Element 23 is 0
Element 24 is 0
-------------8--------------
Element 15 is 88
Element 16 is 7
Element 17 is 8
out_of_range at 18

-------------9--------------
0
-------------10--------------

*/
原文地址:https://www.cnblogs.com/hellokitty2/p/14589256.html