#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<char>a(10), b(10);
cout << a.empty() << ","<< sizeof(a) << ",";
for (char i = 'a', j = 0; j < 10; j++)
a[j] = i + j;
cout << a.max_size() << "," << a.capacity() << "," << a.size() << endl;
a.push_back('k');
cout << a.max_size() << "," << a.capacity() << "," << a.size() << endl;
a.pop_back(); a.pop_back(); a.pop_back(); a.pop_back(); a.pop_back(); a.pop_back();
cout << a.max_size() << "," << a.capacity() << "," << a.size() << endl;
return 0;
}
从上面可以看出一个长度为10的vector放满后再放入会将容量增加50%,虽然教科书上写的是会翻倍到20,但是我的运行环境是win10(64)+vs2015得到的结果是15,
sizeof的结果倒是和教科书是一样的16,max_size也是4294967295,我的可是16G内存啊(后来发现这个是2^32-1,莫非我的vs是32位的?)
另外empty的输出居然是0而不是false,我看了函数的声明返回类型是bool啊
。然后vector变长后就不会变回去了,即使把后面的元素全部删掉后