Solved

std::map update value

Posted on 2013-06-17
10
457 Views
Last Modified: 2013-07-08
How can I update an existing  value inside :
std::map ?
0
Comment
Question by:bachra04
10 Comments
 
LVL 24

Expert Comment

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

Author Comment

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

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

Expert Comment

by:chaau
Comment Utility
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
 
LVL 2

Author Comment

by:bachra04
Comment Utility
Thanks,
One problem, value is an object and I need to delete it ?
0
 
LVL 24

Expert Comment

by:chaau
Comment Utility
It could be better if you provide some code
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 2

Author Comment

by:bachra04
Comment Utility
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 24

Expert Comment

by:chaau
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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 28

Expert Comment

by:pepr
Comment Utility
@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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now