#include <stdio.h>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main()
{
vector<string> vecStr;
cout << "vecStr.size() = " << vecStr.size() << endl;
cout << "vecStr.capacity() = " << vecStr.capacity() << endl;
cout << "---------------\n";
vecStr.push_back("a");
cout << "vecStr.size() = " << vecStr.size() << endl;
cout << "vecStr.capacity() = " << vecStr.capacity() << endl;
cout << "---------------\n";
//当vector空间用完,再插入值时,vector内存空间会以1.5或2倍增长
vecStr.push_back("b");
vecStr.push_back("c");
cout << "vecStr.size() = " << vecStr.size() << endl;
cout << "vecStr.capacity() = " << vecStr.capacity() << endl;
cout << "---------------\n";
//只清空vector,未释放vector内存
vecStr.clear();
cout << "vecStr.size() = " << vecStr.size() << endl;
cout << "vecStr.capacity() = " << vecStr.capacity() << endl;
cout << "---------------\n";
//释放vector内存
vector<string>().swap(vecStr);
cout << "vecStr.size() = " << vecStr.size() << endl;
cout << "vecStr.capacity() = " << vecStr.capacity() << endl;
cout << "---------------\n";
return 0;
}
笔试题 -- vector内存空间释放及vector数据清空
最新推荐文章于 2024-03-26 22:13:28 发布