Sunday 15 March 2009

An example of advanced erase in C++ maps

The following example shows how to use erase in C++ maps


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This program shows an example of how C++ maps erase work.
#include <iostream>
#include <map>

using namespace
std;

int
main ()
{

map<char,int> zgmap;
map<char,int>::iterator it,it_low,it_up;

zgmap['a']=1;
zgmap['b']=2;
zgmap['c']=3;
zgmap['d']=4;
zgmap['e']=5;
zgmap['g']=6;
zgmap['j']=7;
zgmap['n']=8;

it_low=zgmap.lower_bound ('c'); // it_low points to c
it_up=zgmap.upper_bound ('j'); // it_up points to n and not j

zgmap.erase(it_low,it_up); // erases all elements from c to j

//Print Map contents
cout<<"\n** Printing the zgmap contents **"<<endl;
for
( it=zgmap.begin() ; it != zgmap.end(); it++ )
{

cout << (*it).first << " => " << (*it).second << endl;
}


return
0;
}



The output of the program is as follows:

No comments:

Post a Comment