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

原型:
#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;
}

运行结果如下:



  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL(Standard Template Library)是C++标准库的一个重要组成部分。它提供了一系列的模板容器和算法,其迭代器(Iterator)是STL的一个重要概念。 迭代器是一种类似于指针的对象,用于遍历容器的元素。它提供了一组操作,包括解引用、移动等,使得我们可以在不了解容器内部结构的情况下访问和操作容器的元素。 在STL,迭代器分为五种类型: 1. 输入迭代器(Input Iterator):只能读取容器的元素,且只能进行单步前进操作。 2. 输出迭代器(Output Iterator):只能写入容器的元素,且只能进行单步前进操作。 3. 前向迭代器(Forward Iterator):可以读取和写入容器的元素,且可以进行多步前进操作。 4. 双向迭代器(Bidirectional Iterator):可以读取和写入容器的元素,且可以进行多步前进和后退操作。 5. 随机访问迭代器(Random Access Iterator):具有双向迭代器的所有功能,并且支持随机访问和指针算术运算。 对于不同的STL容器,可以使用对应的迭代器类型来进行遍历和操作。例如,对于vector容器可以使用随机访问迭代器,对于list容器可以使用双向迭代器。 以下是一个使用迭代器遍历vector容器的示例代码: ```cpp #include <iostream> #include <vector> int main() { std::vector<int> nums = {1, 2, 3, 4, 5}; // 使用迭代器遍历容器 std::vector<int>::iterator it; for (it = nums.begin(); it != nums.end(); ++it) { std::cout << *it << " "; } return 0; } ``` 在上述代码,我们定义了一个vector容器nums,并使用迭代器it进行遍历。通过调用`begin()`函数可以获取指向容器第一个元素的迭代器,调用`end()`函数可以获取指向容器最后一个元素之后位置的迭代器。在循环,通过解引用操作`*it`来获取迭代器指向的元素的值。 需要注意的是,当遍历结束后,迭代器会指向容器最后一个元素之后的位置,即`nums.end()`。因此,循环条件使用`it != nums.end()`来判断是否遍历完成。 这只是使用迭代器的简单示例,STL还有许多其他功能强大的算法和容器可以与迭代器一起使用。通过灵活运用迭代器,我们可以更方便地操作和处理STL容器的元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值