Skip to content

Instantly share code, notes, and snippets.

@drbobbeaty
Created March 23, 2011 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drbobbeaty/883960 to your computer and use it in GitHub Desktop.
Save drbobbeaty/883960 to your computer and use it in GitHub Desktop.
Test of std::map Iterator invalidation on Ubuntu 10.04.1
#include "map"
#include "string"
#include "exception"
#include "iostream"
using namespace std;
int main(void) {
map<string, string> map_test;
map<string, string>::iterator iter_map_test;
map_test [ "AAAAA" ] = "11111";
map_test [ "BBBBB" ] = "22222";
map_test [ "CCCCC" ] = "33333";
iter_map_test = map_test.find ("BBBBB");
map_test.erase ("BBBBB");
try {
string value = (*iter_map_test).second;
cout << "got : " << value << endl;
++iter_map_test;
cout << "next: " << (*iter_map_test).second << endl;
} catch ( exception & e ) {
cout << e.what() << endl;
} catch ( ... ) {
cout << "generic exception." << endl;
}
return(0);
}
$ g++ maptest.cpp -o maptest
$ maptest
got :
next: 11111
$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment