更新std :: map中的值? [英] Update a value in std::map?

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

问题描述

如果我有:


std :: map< int,intm;


我在哪里:


m.insert(std :: make_pair(1,2));


如何将值更新为7与key = 1相关联?


我在想:


(* m.find(1))。second = 7;


但是没有更简单的方法吗?

If I have:

std::map<int,intm;

where I have:

m.insert(std::make_pair(1,2));

how do I update the value to 7 associated with key = 1?

I was thinking:

(*m.find(1)).second = 7;

But is there no simpler way?

推荐答案

在4íj,13:37,桌面< f ... @ sss.comwrote:
On 4 íj, 13:37, desktop <f...@sss.comwrote:

如果我有:


std :: map< int,intm;


我在哪里:


m.insert(std :: make_pair(1,2));


如何更新与key = 1相关的值为7?


我在想:


(* m.find(1))。second = 7 ;


但是没有更简单的方法吗?
If I have:

std::map<int,intm;

where I have:

m.insert(std::make_pair(1,2));

how do I update the value to 7 associated with key = 1?

I was thinking:

(*m.find(1)).second = 7;

But is there no simpler way?



m [1] = 7;

m[1] = 7;


On4íj,13:38,Ondra Holub< ondra.ho ... @ post.czwrote:
On 4 íj, 13:38, Ondra Holub <ondra.ho...@post.czwrote:

在4íj,13:37,桌面< f ... @ sss.comwrote:
On 4 íj, 13:37, desktop <f...@sss.comwrote:

如果我有:
If I have:


std :: map< int,intm;
std::map<int,intm;


我在哪里:
where I have:


m.insert(std: :make_pair(1,2));
m.insert(std::make_pair(1,2));


如何将值更新为7与key = 1相关联?
how do I update the value to 7 associated with key = 1?


我在想:
I was thinking:


(* m.find( 1))。second = 7;
(*m.find(1)).second = 7;


但是没有更简单的方法吗?
But is there no simpler way?



m [1] = 7;


m[1] = 7;



请注意,operator []有副作用。如果地图中没有该关键值

,则添加新的关键值。但是对于你的使用它应该是

ok。

Please note, operator[] has "side effect". In case, that key value is
not present in map, new one is added. But for your usage it should be
ok.


10月4日晚上9:37,桌面< f。 .. @ sss.comwrote:
On Oct 4, 9:37 pm, desktop <f...@sss.comwrote:

如果我有:


std :: map< int,intm;


我在哪里:


m.insert(std :: make_pair(1,2));


如何将值更新为7与key = 1相关联?


我在想:


(* m.find (1))。second = 7;


但是没有更简单的方法吗?
If I have:

std::map<int,intm;

where I have:

m.insert(std::make_pair(1,2));

how do I update the value to 7 associated with key = 1?

I was thinking:

(*m.find(1)).second = 7;

But is there no simpler way?



是:


m [1] = 7;


但是检查在你这样做之前你有一个价值为
的钥匙,因为如果确实没有,那么它将会被创造出来。


使用map :: find会员查看。


-

Chris Val

Yes:

m[1]=7;

But check that you have a key with a value
of 1 before doing that, because if it does
not exist, it will be created.

Use the map::find member to check it.

--
Chris Val

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

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