请你回答一下STL里resize和reserve的区别
参考回答:
resize():改变当前容器内含有元素的数量(size()),eg: vector<int>v; v.resize(len);v的size变为len,如果原来v的size小于len,那么容器新增(len-size)个元素,元素的值为默认为0.当v.push_back(3);之后,则是3是放在了v的末尾,即下标为len,此时容器是size为len+1;#include <iostream> #include <vector> using namespace std; int main() { vector<int> a; a.reserve(100); a.resize(50); cout<<a.size()<<" "<<a.capacity()<<endl; //50 100 a.resize(150); cout<<a.size()<<" "<<a.capacity()<<endl; //150 200 a.reserve(50); cout<<a.size()<<" "<<a.capacity()<<endl; //150 200 a.resize(50); cout<<a.size()<<" "<<a.capacity()<<endl; //50 200 }