//
01 v.clear(); 只将 v.size() 清零, v.capacity() 则不变
02 swap(); 若与空向量交换 上述两者都清零
// eg.
#include<bits/stdc++.h>
using namespace std;
const int N=11;
vector<int> v;
int main()
{
for( int i=0;i<N;i++ ) v.push_back( i );
cout<<v.size()<<' '<<v.capacity()<<endl;
v.clear();
cout<<v.size()<<' '<<v.capacity()<<endl;
vector<int> temp;
swap( v,temp );
cout<<v.size()<<' '<<v.capacity()<<endl;
return 0;
}
// 可能的输出:
// 11 16
// 0 16
// 0 0
v.clear(); vs swap(); -> v.size(),v.capacity()
最新推荐文章于 2022-03-26 00:45:58 发布