std::map
#include <map> #include <iostream> using namespace std; int main() { std::map<int, std::string> _map = { {0, "00"}, {1, "11"}, {2, "22"} }; // get value std::cout << _map[0] << std::endl; std::cout << _map[1] << std::endl; std::cout << _map[100] << std::endl; std::cout << _map.at(100) << std::endl; auto iter = _map.find(1); if (iter == _map.end()) { std::cout << "iter == _map.end()" << std::endl; } else { std::cout << "iter->second:" << iter->second << std::endl; } // insert value _map[100] = "boom"; _map.insert(std::pair<int, std::string>(4, "44")); std::cout << _map[100] << std::endl; for (auto item = _map.begin(); item != _map.end(); item++) { std::cout << item->second << " "; } std::cout << endl; return 0; }
muhe@muhe-HP:~/cm$ g++ -std=c++11 sd.c
muhe@muhe-HP:~/cm$ ./a.out
00
11
iter->second:11
boom
00 11 22 44 boom

浙公网安备 33010602011771号