map容器find用法

map容器是STL中比较强大的一个container,下面的代码主要讲的是map容器中find函数的用法,

代码取自:http://www.cplusplus.com/reference/map/map/find/

// map::find
#include 
#include 

int main ()
{
  std::map mymap;
  std::map::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:" << '\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 
#include 

int _tmain(int argc, _TCHAR* argv[])
{
	std::map myMap;
	typedef std::map::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<second<second<second<second<

 

转载于:https://www.cnblogs.com/Jasonscor/p/3311430.html

你可能感兴趣的:(map容器find用法)