map容器find⽤法
map容器是STL中⽐较强⼤的⼀个container,下⾯的代码主要讲的是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');
// print content:
std::cout << "elements in mymap:" << '\n';
std::cout << "a => " << mymap.find('a')->second << '\n';
std::cout << "c => " << mymap.find('c')->second << '\n';
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[])
{container容器用法
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;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论