STL中clear()操作方法的使用细节

本文介绍如何正确使用clear()函数来清除vector和map容器中的元素,包括处理对象与指针的不同情况,并提供释放容器容量的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原型:
#include <vector>
void clear();
函数clear()删除储存在vector中的所有元素. 如果vector的元素是一些object, 则它将为当前储存的每个元素调用它们各自的析构函数(destructor). 然而, 如果vector储存的是指向对象的指针, 此函数并不会调用到对应的析构函数. 在第二种情况下, 为了完全删除vector中的元素则应使用一个类似于下的循环:
    std::vector<SomeObject*> aVector;
    //The elements of the vector are created with the operand 'new' at some point in the program
    [...]
    for(int i=0 ; i<aVector.size() ; i++)
        delete aVector[i];
    aVector.clear();
调用clear之后, vector的尺寸(size)将变成zero. 但它的容量(capacity)却并不发生变化, vector本身并不释放任何内存.

如果你想同时做到清空vector的元素和释放vector的容量, 你可以使用swap技巧(此技巧并非在所有环境中都管用 e.g. not with Intel Compiler 10.0.69 and LINUX 2.6.9-89 x64):
    std::vector aVector;
    [...]
    aVector.swap( std::vector() );
这样做会创建一个临时的空vector, 它将替换希望清空的vector.

clear()以线性时间linear time运行.


以vector::clear()为例:

#include<iostream>
#include <vector>

using namespace std;

class Student
{
public:
	int num;
	char *address;
	string name;
};


int main()
{
	// vector::clear()示例
	std::vector<Student*> m_studentVec;
	cout << "Stage1:" << m_studentVec.size() << endl;
	Student s1;
	s1.num = 1;
	Student s2;
	s2.num = 2;
	Student s3;
	s3.num = 3;
	m_studentVec.push_back(&s1);
	m_studentVec.push_back(&s2);
	m_studentVec.push_back(&s3);
	cout << "Stage2:" << m_studentVec.size() << endl;

	// s1、s2、s3是对象,不是new出来的,不需要delete,否则会报错
	m_studentVec.clear();

	cout << "Stage3:" << m_studentVec.size() << endl;
	Student *s4 = new Student();
	s4->num = 4;
	Student *s5 = new Student();
	s5->num = 5;
	Student *s6 = new Student();
	s6->num = 6;
	m_studentVec.push_back(s4);
	m_studentVec.push_back(s5);
	m_studentVec.push_back(s6);
	cout << "Stage4:" << m_studentVec.size() << endl;

	// s4、s5、s6是new出来的,需要delete,如果不delete,只使用clear(),虽然也可以清空,但不会将原vector中的元素的指针置空
	std::vector<Student*>::iterator iter = m_studentVec.begin();
	for (; iter!=m_studentVec.end();iter++)
	{
		if (*iter)
		{
			delete *iter;
			*iter = NULL;
		}
	}
	m_studentVec.clear();

	cout << "Stage5:" << m_studentVec.size() << endl;

	return 0;
}

运行结果如下:



以map::clear()为例:

#include<iostream>
#include <map>

using namespace std;

class Student
{
public:
	int num;
	char *address;
	string name;
};


int main()
{
	// map::clear()示例
	typedef std::map<int, Student*> StudentMap;
	StudentMap m_students;	
	int index = 0;

	cout << "Stage1:" << m_students.size() << endl;
	Student s1;
	s1.num = 1;
	Student s2;
	s2.num = 2;
	Student s3;
	s3.num = 3;
	m_students[index++] = &s1;
	m_students[index++] = &s2;
	m_students[index++] = &s3;
	cout << "Stage2:" << m_students.size() << endl;

	// s1、s2、s3是对象,不是new出来的,不需要delete,否则会报错
	m_students.clear();

	cout << "Stage3:" << m_students.size() << endl;
	Student *s4 = new Student();
	s4->num = 4;
	Student *s5 = new Student();
	s5->num = 5;
	Student *s6 = new Student();
	s6->num = 6;
	m_students[index++] = s4;
	m_students[index++] = s5;
	m_students[index++] = s6;
	cout << "Stage4:" << m_students.size() << endl;

	// s4、s5、s6是new出来的,需要delete,如果不delete,只使用clear(),虽然也可以清空,但不会将原map中的元素的指针置空
	for (StudentMap::iterator iter = m_students.begin();
		iter != m_students.end(); ++iter)
	{
		if (iter->second)
		{
			delete iter->second;
			iter->second = NULL;
		}
	}
	m_students.clear();

	cout << "Stage5:" << m_students.size() << endl;

	return 0;
}

运行结果如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独立游戏开发指南

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值