Tag Archives: std::map

C++ std map

Declare

std::map first;

Add
first[‘a’]=10;
first[‘b’]=30;
first[‘c’]=50;
first[‘d’]=70;

Test key exist:
if ( first.find(“f”) == first.end() ) {
// not found
} else {
// found
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
it = mymap.begin();
while (it != mymap.end()) {
   if (something)
      mymap.erase(it++);
   else
      it++;
}
 
C++ 11
 
std::map<K, V>::iterator itr = myMap.begin();
while (itr != myMap.end()) {
    if (ShouldDelete(*itr)) {
       itr = myMap.erase(itr);
    } else {
       ++itr;
    }
}