Skip to content

Instantly share code, notes, and snippets.

@weidagang
Last active December 14, 2015 05:40
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 weidagang/5036898 to your computer and use it in GitHub Desktop.
Save weidagang/5036898 to your computer and use it in GitHub Desktop.
The example of STL map
#include<map>
#include<string>
#include<iostream>
#include<cassert>
typedef std::map<int, std::string> Map;
int main() {
Map m;
Map::iterator it;
// insert
m[1] = "value1";
m[2] = "value2";
m.insert(Map::value_type(3, "value3"));
// find
it = m.find(10);
assert(it == m.end());
it = m.find(1);
assert(it == m.begin());
// iterate
assert(m.size() == 3);
for (it = m.begin(); it != m.end(); ++it) {
std::cout << it->first << " -> " << it->second << std::endl;
}
std::cout << std::endl;
// erase
m.erase(m.begin());
assert(m.size() == 2);
for (it = m.begin(); it != m.end(); ++it) {
std::cout << it->first << " -> " << m[it->first] << std::endl;
}
std::cout << std::endl;
// update
m[it->first] = "new value";
assert(m.size() == 2);
for (it = m.begin(); it != m.end(); ++it) {
std::cout << it->first << " -> " << m[it->first] << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment