Update a value in std::map?

D

desktop

If I have:

std::map<int,int> m;

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?
 
O

Ondra Holub

If I have:

std::map<int,int> m;

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;
 
C

Chris ( Val )

If I have:

std::map<int,int> m;

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.
 
D

desktop

Chris said:
If I have:

std::map<int,int> m;

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.

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

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

Chris ( Val )

Chris ( Val ) wrote:




If I have:
std::map<int,int> m;
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?

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.

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

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

m[ 1 ] -= 1;
 
D

desktop

Chris said:
Chris ( Val ) wrote:




If I have:
std::map<int,int> m;
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.
Ok if I just want to subtract 1 I can just do:

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

m[ 1 ] -= 1;

Ok but my suggestion also works right - currently not possible to check it.
 
G

Guest

Chris said:
Chris ( Val ) wrote:





If I have:
std::map<int,int> m;
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;

Ok but my suggestion also works right - currently not possible to check it.

Yes, or you can do just '--m[1];'.
 
M

Mark P

desktop said:
Chris said:
Chris ( Val ) wrote:





If I have:
std::map<int,int> m;
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;

Ok but my suggestion also works right - currently not possible to check it.

It works, but it requires two lookups instead of one.
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top