如何更新std :: map使用find方法后? [英] How to update std::map after using the find method?

查看:273
本文介绍了如何更新std :: map使用find方法后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用 find 方法后更新 std :: map 中的键值? p>

我有一个地图和迭代器声明如下:

  map< char,int> m1; 
map< char,int> :: iterator m1_it;
typedef pair< char,int> count_pair;

我使用地图来存储一个字符的出现次数。



我正在使用Visual C ++ 2010。

解决方案

std :: map :: find 返回一个迭代器到找到的元素(或 end()发现)。只要 map 不是const,就可以修改迭代器指向的元素:

  std :: map< char,int> m; 
m.insert(std :: make_pair('c',0)); // c is for cookie

std :: map< char,int> :: iterator it = m.find('c');
if(it!= m.end())
it-> second = 42;


How to update the value of a key in std::map after using the find method?

I have a map and iterator declaration like this:

map <char, int> m1;
map <char, int>::iterator m1_it;
typedef pair <char, int> count_pair;

I'm using the map to store the number of occurrences of a character.

I'm using Visual C++ 2010.

解决方案

std::map::find returns an iterator to the found element (or to the end() if the element was not found). So long as the map is not const, you can modify the element pointed to by the iterator:

std::map<char, int> m;
m.insert(std::make_pair('c', 0));  // c is for cookie

std::map<char, int>::iterator it = m.find('c'); 
if (it != m.end())
    it->second = 42;

这篇关于如何更新std :: map使用find方法后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆