473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Modifying the value of key of a map.

Hi,

I have a map of the type:

std::map<A, B> mymap_A;
std::map<X, A> mymap_X;

where A, B and X are user defined types (i.e. my own clases)

At the beging of the program i do a insert on the maps. Later, at some
point i come across a situation where the value of some member of A
changes. e.g:

class A {
int a_member;
};

when i did an insert a_member was 2; now it is supposed to be 4.

Problem:

my first, and absolutely naive approach was:

std::map<A,B>::iterator i = mymap.find(valueofkey);
i->first = 4;

But this is not allowed as i->first is read only.

Now, the possible solution for this would be to either create a new
pair (and necessarily copy everything in the value part... which is
going to be very costly) and delete old and insert new pair.

Am I missing something too obvious??

Any pointers??

Thnx.
~M

May 30 '06 #1
4 2188
Milind wrote:
Hi,

I have a map of the type:

std::map<A, B> mymap_A;
std::map<X, A> mymap_X;

where A, B and X are user defined types (i.e. my own clases)

At the beging of the program i do a insert on the maps. Later, at some
point i come across a situation where the value of some member of A
changes. e.g:

class A {
int a_member;
};

when i did an insert a_member was 2; now it is supposed to be 4.

Problem:

my first, and absolutely naive approach was:

std::map<A,B>::iterator i = mymap.find(valueofkey);
i->first = 4;

But this is not allowed as i->first is read only.

Now, the possible solution for this would be to either create a new
pair (and necessarily copy everything in the value part... which is
going to be very costly) and delete old and insert new pair.

Am I missing something too obvious??

Any pointers??

Thnx.
~M


The Key type of a map is necessarily a constant. If you want to
"modify" it, the most direct approaches are to delete and re-insert as
you suggested, or use a layer of indirection and let your key have a
pointer to some other data which you can modify freely.

Mark
May 30 '06 #2
How about this?

make the member a_member as mutable. And then modify it through the
iterator.
i->first.a_member = 4;

Or may be through a const member function in class A? This would ensure
that a_member doesn't need to be public.

Just a hack off course.

May 30 '06 #3
shailesh wrote:
How about this?

make the member a_member as mutable. And then modify it through the
iterator.
i->first.a_member = 4;
Please quote. It is hard to know what a_member is without context.
Or may be through a const member function in class A? This would ensure
that a_member doesn't need to be public.

Just a hack off course.


And it may not cut it: we have not been given the details of the classes,
but chances are that a_member takes place in the comparison of keys. If so,
the modification may change the position of this map-entry relative to the
other entries, the map is going to be messed up. In short: you are
modifying a const object and what you earn is undefined behavior.
Best

Kai-Uwe Bux
May 30 '06 #4
Milind wrote:
Hi,

I have a map of the type:

std::map<A, B> mymap_A;
std::map<X, A> mymap_X;

where A, B and X are user defined types (i.e. my own clases)

At the beging of the program i do a insert on the maps. Later, at some
point i come across a situation where the value of some member of A
changes. e.g:

class A {
int a_member;
};

when i did an insert a_member was 2; now it is supposed to be 4.

Problem:

my first, and absolutely naive approach was:

std::map<A,B>::iterator i = mymap.find(valueofkey);
i->first = 4;

But this is not allowed as i->first is read only.

Now, the possible solution for this would be to either create a new
pair (and necessarily copy everything in the value part... which is
going to be very costly) and delete old and insert new pair.

Am I missing something too obvious??


It's hard to tell without looking into your intentions, and perhaps at classes B and X.
Maybe you mixed up the Key and Value parts, or you want something other than a map<A,B>, or...

The basic idea of a map<Key,Value> is that you do (fast) lookup by key,
but you can edit the data freely.
Like
mymap[player.id()].lives--;
or
mymapiter i = mymap.find(player.id()); // mymapiter is supposed to be a (badly named) typedef
if (i != mymap.end()) // if you use find, you must test, otherwise you crash on dereference
i->lives--;

player.id() is the key here. The data is a struct with an int-like member 'lives'.
If you use any other key, you should be looking at other data, ie the data of another player.
And obviously, the id shouldn't change (if it does "id" is probably a bad name for it).
This is what std::maps are supposed to be good for.

If you want to look up by data, eg to find all players with zero lives,
you need something different (perhaps a bidirectional map - is this why you
have both map<A,B> and map<X,A> ? - no way for us to know...)

HTH
homsan
May 30 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jason Heyes | last post by:
Look at my code: void modify_element(BigType &x) { /* not shown */ } BigType modify_element_copy(BigType x) { modify_element(x);
9
by: Stefan Mueller | last post by:
I'd like to set a variable called 'FocusIsOn' if a button got the focus. Because my button is dynamically created I do it like xelement = document.createElement("input") xelement.type = "button"...
3
by: suresh | last post by:
Hi all, I have a hash table and I want to modify the value of a key. Please let me know how can that be acheived. Thanks Suresh
3
by: Chris | last post by:
I have a dataset that I want to modify the values in a particular column. I.e if I have a function to convert the enterbyID (Integer) to a name, in the dataset the field (Concern_EnteredbyID) shows...
3
by: Chris Bingham | last post by:
Hi, I'm learning VB.Net at the moment, and while I'm doing it I'm writing a couple of programs for work! They all work with the same Access Database, but I'm having a problem with modifying...
2
by: bandroo | last post by:
Hi Guys How can I modify the items within a hashtable "in situ" so to speak? At the moment, I am locating the item that I want, extracting it, modifying the item, deleting the hashtable item,...
6
by: tuxedo | last post by:
I have a fixed html structure, where only one form and a simple select menu will exist on an html page, as follows: <form action="order" method="POST"> <select name="dinner"> <option...
7
by: Ivan Voras | last post by:
For a declaration like: List<MyTypeitems = ... where MyType is a struct, attempt to modify an item with items.member = something; fails with the message:
24
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be...
5
by: IUnknown | last post by:
Ok, we are all aware of the situation where modifying the folder structure (adding files, folders, deleting files, etc) will result in ASP.NET triggering a recompilation/restart of the application....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.