Here is some code I half stole from the c++ referance website and filled in a little for my purposes. Initially I wanted to make sure I understood the equal_range function:
this is my understanding of the code below.
calling equal_range returns something that looks like : <<'b',20>, <c,30>>
it becomes <'b',20>
when it gets to the loop, it gets set to <'b',20> again
calling erase should remove the "b" key and its value from the map.
and my output should look like
a->10
b->0
c->30
d->40
e->50
f->60
unfortunately it looks like this:
a->10
b->0
c->30
d->0
e->0
f->0
in fact, a,c and e work as expected, removing the equal_range parameter.
the rest have odd functionality. F erases the whole map, which sorta makes sense to me, but b, and d have me stumped.
any thoughts on this? Also, I know this is not the best way to accomplish this, I wrote this to help better understand a much larger project.
Thanks!
Code:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<char,int> * mymap = new map<char,int>();
map<char,int>::iterator it;
pair<map<char,int>::iterator,map<char,int>::iterator> ret;
bool yes = true;
// insert some values:
(*mymap)['a']=10;
(*mymap)['b']=20;
(*mymap)['c']=30;
(*mymap)['d']=40;
(*mymap)['e']=50;
(*mymap)['f']=60;
ret = mymap->equal_range('b');
it = ret.first;
if(it != mymap->end()){
cout << "lower bound points to: ";
cout << ret.first->first << " => " << ret.first->second << endl;
cout << "upper bound points to: ";
cout << ret.second->first << " => " << ret.second->second << endl;
for(it=ret.first; it!=ret.second; ++it){
if(yes){
mymap->erase(it);
cout << "erased the iter:"<< it->first << endl;
}
}
for(int i = 'a'; i<='f'; i++){
cout << char(i) << "->" << (*mymap)[i] << endl;
}
}
return 0;
}