Solved

std::map update value

Posted on 2013-06-17
10
480 Views
Last Modified: 2013-07-08
How can I update an existing  value inside :
std::map ?
0
Comment
Question by:bachra04
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 25

Expert Comment

by:chaau
ID: 39254757
The easiest you can do is to use operator[]
0
 
LVL 2

Author Comment

by:bachra04
ID: 39254794
do you have sample code ? I have the key
std::map<key , value)

and wants to update the value for that key.
0
 
LVL 25

Expert Comment

by:chaau
ID: 39254838
Here is the sample:

std::map<std::string, int> mymap;
mymap["a"] = 1;
mymap["a"] = 2;

Open in new window


After the line mymap["a"] = 1 the value for element "a" will be 1, after the line mymap["a"] = 2 the value for the element "a" will be 2.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:bachra04
ID: 39254859
Thanks,
One problem, value is an object and I need to delete it ?
0
 
LVL 25

Expert Comment

by:chaau
ID: 39254871
It could be better if you provide some code
0
 
LVL 2

Author Comment

by:bachra04
ID: 39254887
typedef std::map<id, MyObj> MyMap;

MyObj obj1 = new MyObj("first object");
MyMap.insert(1, obj1);



MyObj obj2 = new MyObj("second object");

now I want to replace obj1 with obj2:

???

something like :
MyMap.delete(1)
MyMap.insert(1, obj2);
0
 
LVL 25

Expert Comment

by:chaau
ID: 39254910
Hold on. There is a problem with your code.
When you use MyObj obj1 = new MyObj("first object"); obj1 must be a reference.
So, the code should be:
MyObj *obj1 = new MyObj("first object");

Open in new window

Similarly, typedef std::map<id, MyObj> MyMap; should be:
typedef std::map<id, MyObj*> MyMap;

In this case, you need to use at() method first to check if the element exists. Then you need to delete the object that is currently there. And then you assign a new reference.
The code will be something like this:

typedef std::map<id, MyObj*> MyMap;
MyObj *obj1 = new MyObj("first object");
MyMap.insert(1, obj1);

MyObj *obj2 = new MyObj("second object");
 try {
    MyObj *obj3 = MyMap.at(1);      // map::at throws an out-of-range if key is not found
    delete obj3;
  }
  catch (const std::out_of_range& oor) {
    std::cerr << "Out of Range error: " << oor.what() << '\n'; // you can ignore this error. It just indicate that there is no element with the key = 1 in your map
  }
MyMap[1] = obj2;

Open in new window

0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39254968
If you want to delete an entry from a map, you'll need an iterator poiting to it. That one you can obtain via 'find', i.e.

MyMap mymap;

//...

MyMap::iterator i = mymap.find("First Object");

if (i != mymap.end()) mymap.erase(i);

Open in new window

0
 
LVL 34

Expert Comment

by:sarabande
ID: 39255385
typedef std::map<int, MyObj> MyMap;

Open in new window


is much better than using pointers cause all objects would be deleted automatically when the map was deleted. otherwise you would need to iterate all entries and delete the pointers.

to insert a new element to the map you would do:

MyMap mymap;
...
mymap[1] = MyObj("first object");

Open in new window


the map would add a copy of the temporary MyObj("first object") to the container.

to delete all objects you can call std::map::clear() member function.

mymap.clear();

Open in new window


Sara
0
 
LVL 29

Expert Comment

by:pepr
ID: 39255422
@barchra04: To add to Sara's and chaau's comments... It seems you have some experience with Java or the like language where all objects must be allocated via new and where you always access them via references. There is more straightforward way to work with objects in C++.

You can work directly with the object -- its name is converted by the compiler to the memory address. Also, there is no implicit garbage collection mechanism and reference counting in C++. However, the object that were not allocated via new can destroy themselves when they are stopped to be used.

For the example with the map<>, if you have the object stored inside the map (i.e. not the reference, nor the pointer), and you delete the map element, the object is going to be destroyed automatically.

On the contrary, when storing the pointer in the map element, you have to get the pointer value from the map, call the delete with the pointer argument explicitly, and only then to delete the map element.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question