bachra04
asked on
std::map update value
How can I update an existing value inside :
std::map ?
std::map ?
The easiest you can do is to use operator[]
ASKER
do you have sample code ? I have the key
std::map<key , value)
and wants to update the value for that key.
std::map<key , value)
and wants to update the value for that key.
Here is the sample:
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.
std::map<std::string, int> mymap;
mymap["a"] = 1;
mymap["a"] = 2;
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.
ASKER
Thanks,
One problem, value is an object and I need to delete it ?
One problem, value is an object and I need to delete it ?
It could be better if you provide some code
ASKER
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);
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:
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:
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");
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;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
typedef std::map<int, MyObj> MyMap;
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");
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();
Sara
@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.
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.