map容器是STL中比较强大的一个container,下面的代码主要讲的是map容器中find函数的用法,
代码取自:http://www.cplusplus.com/reference/map/map/find/
// map::find #include <iostream> #include <map> int main () { std::map<char,int> mymap; std::map<char,int>::iterator it; mymap['a']=50; mymap['b']=100; mymap['c']=150; mymap['d']=200; it=mymap.find('b'); mymap.erase (it); mymap.erase (mymap.find('d')); // print content: std::cout << "elements in mymap:" << ' '; std::cout << "a => " << mymap.find('a')->second << ' '; std::cout << "c => " << mymap.find('c')->second << ' '; return 0; }
Output:
elements in mymap: a => 50 c => 150
下面部分是我自己写的代码:
// test_map.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <map> int _tmain(int argc, _TCHAR* argv[]) { std::map<char, unsigned int> myMap; typedef std::map<char, unsigned int>::iterator mapIter; myMap['a'] = 20; myMap['b'] = 30; myMap['c'] = 60; myMap['d'] = 70; myMap.find('a')->second = 120; myMap.find('b')->second = 130; myMap.find('c')->second = 160; myMap.find('d')->second = 170; mapIter it; it = myMap.find('a'); std::cout<<it->second<<std::endl; it = myMap.find('b'); std::cout<<it->second<<std::endl; it = myMap.find('c'); std::cout<<it->second<<std::endl; it = myMap.find('d'); std::cout<<it->second<<std::endl; return 0; }