Solved

std::map update value

Posted on 2013-06-17
10
462 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
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 24

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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 2

Author Comment

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

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 24

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 33

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 28

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

770 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