vector< int>vec;
{
vector< int> x;
vec.swap(x);
}
Clear content Removes all elements from the vector (which are destroyed), leaving the container with a size of 0.
A reallocation is not guaranteed to happen, and the vector capacity is not guaranteed to change due to calling this function. A typical alternative that forces a reallocation is to use swap: vector().swap(x); // clear x reallocating
#include<iostream>
#include<vector>
using namespace std;
int main(void)
{
vector<int> vec; //创建动态数组,未知大小
vec.push_back(1); //添加元素1,此时为{1}
vec.push_back(2); //添加元素2,此时为{1,2}
vec.push_back(3); //添加元素3,此时为{1,2,3}
vec[1] = 3; //下标为1的元素被修改为3,此时为{1,3,3}
vec[2] = 2; //下标为2的元素被修改为2, 此时为{1,3,2}
for (int i = 0; i < vec.size(); i++)
printf("%d\n",vec[i]);
return 0;
}