Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

std::map update value

Posted on 2013-06-17
10
Medium Priority
?
501 Views
Last Modified: 2013-07-08
How can I update an existing  value inside :
std::map ?
0
Comment
Question by:bachra04
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
Technology Partners: 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 2000 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 35

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

782 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