Link to home
Start Free TrialLog in
Avatar of bachra04
bachra04Flag for Canada

asked on

std::map update value

How can I update an existing  value inside :
std::map ?
Avatar of chaau
chaau
Flag of Australia image

The easiest you can do is to use operator[]
Avatar of bachra04

ASKER

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

and wants to update the value for that key.
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.
Thanks,
One problem, value is an object and I need to delete it ?
It could be better if you provide some code
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);
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

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of pepr
pepr

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