删除map容器中指定的元素

// testDelMap.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>
#include <map>
#include <string>

using namespace std;

/*
标题:删除map容器中指定的元素。
作者:kagula@20150402
环境:VS2010SP1
*/
int _tmain(int argc, _TCHAR* argv[])
{
	map<string,string> mapT;

	//create sample
	mapT["A"]="A";
	mapT["B"]="A";
	mapT["C"]="B";
	mapT["D"]="B";
	mapT["E"]="A";

	//remove specified elements in the container!
	map<string,string>::iterator iter = mapT.begin();
	while (iter!=mapT.end())
	{
		if (iter->second  == "B")
		{
			mapT.erase(iter++);
		}
		else
		{
			iter++;
		}
	}

	//print result
	iter = mapT.begin();
	while (iter!=mapT.end())
	{
		cout<<iter->second<<endl;

		iter++;
	}
	cin.get();
	return 0;
}

你可能感兴趣的:(删除map容器中指定的元素)