vector

////////////////////////////////////////
//      2018/04/15 19:12:27
//      vector - capacity(容量)

/*
 Size指目前容器中实际有多少元素,对应的resize(size_type)会在容器尾添加或删除一些元素,
 来调整容器中实际的内容,使容器达到指定的大小。

 Capacity指最少要多少元素才会使其容量重新分配,对应reserve(size_type new_size)
 会这置这个capacity值,使它不小于所指定的new_size。

    故用reserve(size_type)只是扩大capacity值,这些内存空间可能还是“野”的,如果此时使用"[ ]"来
访问,则可能会越界。而resize(size_type new_size)会真正使容器具有new_size个对象。
*/
#include <iostream>
#include <vector>

using namespace std;

int main(){
    vector<int> v(10);

    cout << "Size of v = " << v.size() << endl;
    cout << "Capacity of v =" << v.capacity() << endl;

    v.resize(100);

    cout << "After resizing:" << endl;
    cout << "Size of v = " << v.size() << endl;
    cout << "Capacity of v = " << v.capacity() << endl;

    return 0;
}
/*
OUTPUT:
    Size of v = 10
    Capacity of v =10
    After resizing:
    Size of v = 100
    Capacity of v = 100
*/ 
原文地址:https://www.cnblogs.com/laohaozi/p/12538053.html