473,385 Members | 1,472 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Update a value in std::map?

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?
Oct 4 '07 #1
10 55939
On 4 íj, 13:37, desktop <f...@sss.comwrote:
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;

Oct 4 '07 #2
On 4 íj, 13:38, Ondra Holub <ondra.ho...@post.czwrote:
On 4 íj, 13:37, desktop <f...@sss.comwrote:
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;
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.

Oct 4 '07 #3
On Oct 4, 9:37 pm, desktop <f...@sss.comwrote:
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?
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
Oct 4 '07 #4
Chris ( Val ) wrote:
On Oct 4, 9:37 pm, desktop <f...@sss.comwrote:
>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?

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

Ok if I just want to subtract 1 I can just do:

m[1] = m[1]-1;
Oct 4 '07 #5
On Oct 4, 11:38 pm, desktop <f...@sss.comwrote:
Chris ( Val ) wrote:


On Oct 4, 9:37 pm, desktop <f...@sss.comwrote:
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?
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

Ok if I just want to subtract 1 I can just do:

m[1] = m[1]-1;
m[ 1 ] -= 1;

--
Chris Val

Oct 4 '07 #6
Chris ( Val ) wrote:
On Oct 4, 11:38 pm, desktop <f...@sss.comwrote:
>Chris ( Val ) wrote:


>>On Oct 4, 9:37 pm, desktop <f...@sss.comwrote:
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?
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
Ok if I just want to subtract 1 I can just do:

m[1] = m[1]-1;

m[ 1 ] -= 1;

--
Chris Val
Ok but my suggestion also works right - currently not possible to check it.
Oct 4 '07 #7
On 2007-10-04 19:13, desktop wrote:
Chris ( Val ) wrote:
>On Oct 4, 11:38 pm, desktop <f...@sss.comwrote:
>>Chris ( Val ) wrote:

On Oct 4, 9:37 pm, desktop <f...@sss.comwrote:
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?
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
Ok if I just want to subtract 1 I can just do:

m[1] = m[1]-1;

m[ 1 ] -= 1;

--
Chris Val

Ok but my suggestion also works right - currently not possible to check it.
Yes, or you can do just '--m[1];'.

--
Erik Wikström
Oct 4 '07 #8
desktop wrote:
Chris ( Val ) wrote:
>On Oct 4, 11:38 pm, desktop <f...@sss.comwrote:
>>Chris ( Val ) wrote:

On Oct 4, 9:37 pm, desktop <f...@sss.comwrote:
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?
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
Ok if I just want to subtract 1 I can just do:

m[1] = m[1]-1;

m[ 1 ] -= 1;

--
Chris Val

Ok but my suggestion also works right - currently not possible to check it.
It works, but it requires two lookups instead of one.
Oct 4 '07 #9
Mark P wrote:
It works, but it requires two lookups instead of one.
I'm wondering if any compiler is smart enough to optimize the second
lookup away.
Oct 4 '07 #10
On 2007-10-04 20:37, Juha Nieminen wrote:
Mark P wrote:
>It works, but it requires two lookups instead of one.

I'm wondering if any compiler is smart enough to optimize the second
lookup away.
It would be hard, since there is not guarantee (from the language) that
the reference returned the second time will be the same as on the first
call. A human can quite easily see that it will be, but I am not sure if
the compiler can.

--
Erik Wikström
Oct 5 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: EnTn | last post by:
Hi Everyone... I've been trying to use a std::map to do some storage. Basically, i'm storing double values using a Key Object. The Key object is quite simple and is just a pair of int's...
14
by: Flzw | last post by:
Well I have a map like this : std::map <string, CObject> ObjectList; I have a function like this : CObject* NewObject( char* Name, CArg* Arg) { std::string key = Name; ObjectList =...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
4
by: Raider | last post by:
Is there std::map member-function that do as code below? typedef std::map<NameClass, ValueClass> ParameterContainer; .... // this code is equivalent to "_Parameters = Value", // but a bit...
21
by: aaragon | last post by:
Hello everyone, I would like to know if there is a way to use the std::map to store different types for one of its two types. That is, I'm trying to use it as: typedef...
2
by: Fei Liu | last post by:
Hello, I am getting something really wierd out of this source code, can you please verify if you also get segmentation fault (memory failure)? http_form.h: (sorry about the line wrapping, email...
5
by: Diwa | last post by:
Does the "value" type (value as in key-value pair )of "std::map" require a default ctor even if it is not used ? If I comment out Line 1 in the code attached later, i.e remove the default ctor...
7
by: Matthias Pfeifer | last post by:
Hi there, a std::map is build from std::pair elements where for a pair p p.first is called the key and p.second the value. Is there a way to keep the map sorted by the values? Ie is there a...
5
by: Chris Forone | last post by:
Hello group, g++ (3.4.2, mingw): float init = {1.f, 2.f, 3.f}; std::map<std::string, std::valarray<float mp; mp = std::valarray(init, 3); mp.size(); // should be 3, but IS 0!
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.