STL vector清空元素的三种方法
1. clear()方法
作用:
清空元素,但不回收空间。
vector有两个参数: 1.size,表示当前vector容器内存储的元素个数; 2. capacity,表示当前vector在内存中申请的这片区域所能容纳的元素个数。
通常capacity会比size大,如果往vector中push_back数据,这样就不用重新申请内存和拷贝元素到新内存区域了,便于节省时间。
所以vector.clear()的真正作用是:把size设置成0,capacity不变。代码示例:
/*------------------------------------------代码--------------------------------------------------*/
#include <iostream>
#include <fstream>
#include <memory>
#include <vector>
#include <string.h>
using namespace std;
int main()
{
std::vector<double> vecnum;
cout << "init: size = " << vecnum.size() << "; capacity = " << vecnum.capacity() << endl;
vecnum.push_back(1.111);
vecnum.push_back(2.222);
vecnum.push_back(3.333);
vecnum.push_back(4.333);
cout << "after push_back: size = " << vecnum.size() << "; capacity = " << vecnum.capacity() << endl;
vecnum.clear();
cout << "after clear: size = " << vecnum.size() << "; capacity = " << vecnum.capacity() << endl;
return 0;
}
// 结果
init: size = 0; capacity = 0
after push_back: size = 4; capacity = 4
after clear: size = 0; capacity = 4
/*------------------------------------------代码--------------------------------------------------*/
2. erase方法
作用:清空元素,但不回收空间
/*------------------------------------------代码--------------------------------------------------*/
#include <iostream>
#include <fstream>
#include <memory>
#include <vector>
#include <string.h>
using namespace std;
int main()
{
std::vector<double> vecnum;
cout << "init: size = " << vecnum.size() << "; capacity = " << vecnum.capacity() << endl;
vecnum.push_back(1.111);
vecnum.push_back(2.222);
vecnum.push_back(3.333);
vecnum.push_back(4.333);
cout << "after push_back: size = " << vecnum.size() << "; capacity = " << vecnum.capacity() << endl;
vector<double>::iterator it;
for (it = vecnum.begin(); it != vecnum.end(); )
{
//注意使用erase()方法时,迭代不要随意的++
it = vecnum.erase(it);
}
cout << "after erase: size = " << vecnum.size() << "; capacity = " << vecnum.capacity() << endl;
return 0;
}
// 结果
init: size = 0; capacity = 0
after push_back: size = 4; capacity = 4
after clear: size = 0; capacity = 4
/*------------------------------------------代码--------------------------------------------------*/
注意:erase在每次操作时,迭代器指针会整体前移1,就是每次都会移动全部数据,所以vector不适合做频繁删除的容器
3. swap方法
作用:清除元素并回收内存
/*------------------------------------------代码--------------------------------------------------*/
#include <iostream>
#include <fstream>
#include <memory>
#include <vector>
#include <string.h>
using namespace std;
int main()
{
std::vector<double> vecnum;
cout << "init: size = " << vecnum.size() << "; capacity = " << vecnum.capacity() << endl;
vecnum.push_back(1.111);
vecnum.push_back(2.222);
vecnum.push_back(3.333);
vecnum.push_back(4.333);
cout << "after push_back: size = " << vecnum.size() << "; capacity = " << vecnum.capacity() << endl;
vector<double>().swap(vecnum);
cout << "after swap: size = " << vecnum.size() << "; capacity = " << vecnum.capacity() << endl;
return 0;
}
// 结果
init: size = 0; capacity = 0
after push_back: size = 4; capacity = 4
after swap: size = 0; capacity = 0
/*------------------------------------------代码--------------------------------------------------*/