【发布时间】:2019-03-14 18:11:59
【问题描述】:
#include<iostream>
using namespace std;
#include<vector>
#include "s.h"
template<class T>
void f(vector<T> a)
{
cout << "size " << a.size() << endl;
cout << "capacity " << a.capacity() << endl << endl;
}
int main()
{
vector<int> s(10,5);
f(s);
s.resize(50);
f(s);
s.reserve(150);
f(s);
return 0;
}
我认为 resize() 会改变大小,而 reserve() 会改变容量,但在我的示例中,reserve() 不会改变容量,为什么? 还有我的第二个问题——assign() 是什么意思?我可以对 operator=() 做同样的事情吗?
// 10 10
// 50 50
// 50 50
【问题讨论】:
-
查看上述帖子中的答案。 stackoverflow.com/a/7397862/1891604