STL容器的遍历删除操作erase

#include <vector>
#include <list>
#include <set>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;

bool greater6 ( int value ) {
	return value > 6;
}

template <class T> class is_odd : public std::unary_function<T, bool> 
{
public:
	bool operator() ( T& val ) 
	{
		return ( val % 2 ) == 1;
	}
};

int main()
{
	//--- vector.remove_if
	using namespace std;
	vector <int> v1;
	vector <int>::iterator Iter1, new_end;

	int i;
	for ( i = 0 ; i <= 9 ; i++ )
		v1.push_back( i );

	int ii;
	for ( ii = 0 ; ii <= 3 ; ii++ )
		v1.push_back( 7 );

	random_shuffle ( v1.begin( ), v1.end( ) );
	cout << "Vector v1 is ( " ;
	for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
		cout << *Iter1 << " ";
	cout << ")." << endl;

	// Remove elements satisfying predicate greater6
	new_end = remove_if (v1.begin( ), v1.end( ), greater6 );

	cout << "Vector v1 with elements satisfying greater6 removed is\n ( " ;
	for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
		cout << *Iter1 << " ";
	cout << ")." << endl;

	// To change the sequence size, use erase
	v1.erase (new_end, v1.end( ) );

	cout << "Vector v1 resized elements satisfying greater6 removed is\n ( " ;
	for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
		cout << *Iter1 << " ";
	cout << ")." << endl;

	//--- list.remove_if
	list <int> c1;
	list <int>::iterator c1_Iter, list_new_end;

	c1.push_back( 3 );
	c1.push_back( 4 );
	c1.push_back( 5 );
	c1.push_back( 6 );
	c1.push_back( 7 );
	c1.push_back( 8 );

	cout << "The initial list is c1 =";
	for ( c1_Iter = c1.begin( ); c1_Iter != c1.end( ); c1_Iter++ )
		cout << " " << *c1_Iter;
	cout << endl;

#if 0 // list.remove_if:直接删除
	list <int> c2 = c1;
	c2.remove_if( is_odd<int>( ) );

	cout << "After removing the odd elements, "
		<< "the list becomes c2 =";
	for ( c2_Iter = c2.begin( ); c2_Iter != c2.end( ); c2_Iter++ )
		cout << " " << *c2_Iter;
	cout << endl;
#else // std.remove_if:需list.erase配合
	list_new_end = remove_if (c1.begin( ), c1.end( ), is_odd<int>());

	c1.erase (list_new_end, c1.end( ) );
#endif

	//--- vector.erase/list.erase/set.erase/map.erase
	std::vector<int> vtData;
	vtData.push_back(0);
	vtData.push_back(1);
	vtData.push_back(2);
	vtData.push_back(2);
	vtData.push_back(4);

	//

	std::vector<int> vtDemo;
	{
		vtDemo.clear();
		vtDemo.insert(vtDemo.begin(), vtData.begin(), vtData.end());
		for (int i = 0; i < (int)vtDemo.size(); i++)
		{
			if (vtDemo[i] == 2)
			{
				vtDemo.erase(vtDemo.begin() + i);
				i--;
			}
		}

		vtDemo.clear();
		vtDemo.insert(vtDemo.begin(), vtData.begin(), vtData.end());
		for (int i = (int)(vtDemo.size() - 1); i >= 0; i--)
		{
			if (vtDemo[i] == 2)
			{
				vtDemo.erase(vtDemo.begin() + i);
			}
		}

		vtDemo.clear();
		vtDemo.insert(vtDemo.begin(), vtData.begin(), vtData.end());
		for (std::vector<int>::iterator iter = vtDemo.begin(); iter != vtDemo.end();)
		{
			if (*iter == 2)
				iter = vtDemo.erase(iter);
			else
				iter++;
		}
 
		/* // 只有vector的这种形式不可以,set、list、map可以
		vtDemo.clear();
		vtDemo.insert(vtDemo.begin(), vtData.begin(), vtData.end());
		for (std::vector<int>::iterator iter = vtDemo.begin(); iter != vtDemo.end();)
		{
			if (*iter == 2)
				vtDemo.erase(iter++);
			else
				iter++;
		}
		*/
	}

 	std::list<int> lsDemo;
	{
		lsDemo.clear();
 		lsDemo.insert(lsDemo.begin(), vtData.begin(), vtData.end());
		for (std::list<int>::iterator iter = lsDemo.begin(); iter != lsDemo.end();)
		{
			if (*iter == 2)
				iter = lsDemo.erase(iter);
			else
				iter++;
		}

		lsDemo.clear();
		lsDemo.insert(lsDemo.begin(), vtData.begin(), vtData.end());
		for (std::list<int>::iterator iter = lsDemo.begin(); iter != lsDemo.end();)
		{
			if (*iter == 2)
				lsDemo.erase(iter++);
			else
				iter++;
		}
	}

	std::set<int> setDemo;
	{
		setDemo.clear();
		setDemo.insert(vtData.begin(), vtData.end());
		for (std::set<int>::iterator iter = setDemo.begin(); iter != setDemo.end();)
		{
			if (*iter == 2 || *iter == 4)
				iter = setDemo.erase(iter);
			else
				iter++;
		}

		setDemo.clear();
		setDemo.insert(vtData.begin(), vtData.end());
		for (std::set<int>::iterator iter = setDemo.begin(); iter != setDemo.end();)
		{
			if (*iter == 2 || *iter == 4)
				setDemo.erase(iter++);
			else
				iter++;
		}
	}

	std::map<int, int> mapDemo;
	{
		mapDemo.clear();
		for (size_t i = 0; i < vtData.size(); i++)
		{
			mapDemo[i] = vtData[i];
		}

		for (std::map<int, int>::iterator iter = mapDemo.begin(); iter != mapDemo.end();)
		{
			if (iter->second == 2)
				iter = mapDemo.erase(iter);
			else
				iter++;
		}

		mapDemo.clear();
		for (size_t i = 0; i < vtData.size(); i++)
		{
			mapDemo[i] = vtData[i];
		}

		for (std::map<int, int>::iterator iter = mapDemo.begin(); iter != mapDemo.end();)
		{
			if (iter->second == 2)
				mapDemo.erase(iter++);
			else
				iter++;
		}
	}

    return 0;
}


总结

1). std::remove_if
  • 1. 只用于线性集合(vector、list),关联集合(set、map)不可用;
  • 2. std::remove_if只将符合条件的数据移到集合的前段,然后返回冗余后段的起始索引,真正删除数据需vector.erase或list.erase的配合;
  • 3. list提供有自己的remove_if版本,会直接删除符合条件的数据,无需list.erase的配合;vector没有自己的版本;
参考:
https://msdn.microsoft.com/en-us/library/ky68xzbe.aspx
https://msdn.microsoft.com/en-us/library/9fyx5kx8.aspx
http://blog.sina.com.cn/s/blog_68c02fb90100xhoi.html
http://huycwork.blog.163.com/blog/static/136751999201052044123998/
http://www.cnblogs.com/dongzhiquan/archive/2011/12/06/2278658.html
http://blog.csdn.net/vivi_wang_11/article/details/7441803

2). 迭代器的遍历删除
1. 两种形式,
  • 形式一 if (满足条件) { vtDemo.erase(iter++) } :vector的竟然不行!!list、set、map可以;
  • 形式二 if (满足条件) { iter = vtDemo.erase(iter)} :vector、list、set、map都适用!所以还是形式二比较通用!!
参考:
http://blog.csdn.net/lmh12506/article/details/9167653
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值