set inside a map

M

Milind

Hi,

I trying to use map as: map< typeforkey, set<typeofset> > .
This map is a part of a class which has many set, get functions and
other interfaces.

Problem:
I have a set function for this map which has a signature of type:

void foo ( const typeofkey& key, const typeofset& element);

I intend to do an insert which would look something like:
set<typeofset> myset;
myset.insert(element);

myHash.insert( std::pair<typeofkey, map<typeofset> > (key, myset));

now the problem is:
1. Is there a better way of doing it, interms of explicit creation of
the map Vs the environment creating a temporary for itself??

2. If i do insert it like this to the map will be copied to the map,
i.e. all of the elements of the set copied to the new location. i.e.
inside the map?? do i have problems of dangling references?? out of
scope variables?? Can i get a defenetive url or web resource on this?

~ Moh
 
M

Milind

oops i meant,

2. If i do insert it like this to the map, the set be will be copied to
the map,
i.e. all of the elements of the set copied to the new location. i.e.
inside the map?? do i have problems of dangling references?? out of
scope variables?? Can i get a defenetive url or web resource on this?
 
J

Jim Langston

Milind said:
Hi,

I trying to use map as: map< typeforkey, set<typeofset> > .
This map is a part of a class which has many set, get functions and
other interfaces.

Problem:
I have a set function for this map which has a signature of type:

void foo ( const typeofkey& key, const typeofset& element);

I intend to do an insert which would look something like:
set<typeofset> myset;
myset.insert(element);

myHash.insert( std::pair<typeofkey, map<typeofset> > (key, myset));

now the problem is:
1. Is there a better way of doing it, interms of explicit creation of
the map Vs the environment creating a temporary for itself??

2. If i do insert it like this to the map will be copied to the map,
i.e. all of the elements of the set copied to the new location. i.e.
inside the map?? do i have problems of dangling references?? out of
scope variables?? Can i get a defenetive url or web resource on this?

If this was me I would insert my key and a blank set, then get a reference
to the set in the map and insert into it, just so the set doesn't get
copied.
 
I

Ivan Vecerina

:
: : > I trying to use map as: map< typeforkey, set<typeofset> > .
....
: > I intend to do an insert which would look something like:
: > set<typeofset> myset;
: > myset.insert(element);
: >
: > myHash.insert( std::pair<typeofkey, map<typeofset> > (key, myset));
: >
: > now the problem is:
: > 1. Is there a better way of doing it, interms of explicit creation of
: > the map Vs the environment creating a temporary for itself??
: >
: > 2. If i do insert it like this to the map will be copied to the map,
: > i.e. all of the elements of the set copied to the new location. i.e.
: > inside the map?? do i have problems of dangling references?? out of
: > scope variables?? Can i get a defenetive url or web resource on this?
:
: If this was me I would insert my key and a blank set, then get a
reference
: to the set in the map and insert into it, just so the set doesn't get
: copied.

If we are talking about std::map, operator[] pretty much already does
this, so you can simply write:

myHash[key].swap( myset );


hth --ivan
 
M

Milind

Hello Ivan,

Not very clear. could you kindly point me to where i can get the
detials..
insert on msdn, doesn't tell a lot about swap :(

~M
 
I

Ivan Vecerina

: Not very clear. could you kindly point me to where i can get the
: detials..
: insert on msdn, doesn't tell a lot about swap :(

Well, since you removed all context, it doesn't help me reply ;)

Re-quoting:
:: > I trying to use map as: map< typeforkey, set<typeofset> > .
: ...
:: > I intend to do an insert which would look something like:
:: > set<typeofset> myset;
:: > myset.insert(element);
:: >
:: > myHash.insert( std::pair<typeofkey, map<typeofset> > (key, myset));
:: >
:: > now the problem is:
:: > 1. Is there a better way of doing it, interms of explicit creation of
:: > the map Vs the environment creating a temporary for itself??
:: >
:: > 2. If i do insert it like this to the map will be copied to the map,
:: > i.e. all of the elements of the set copied to the new location. i.e.
:: > inside the map?? do i have problems of dangling references?? out of
:: > scope variables?? Can i get a defenetive url or web resource on
this?
::
:: If this was me I would insert my key and a blank set, then get a
: reference
:: to the set in the map and insert into it, just so the set doesn't get
:: copied.
:
: If we are talking about std::map, operator[] pretty much already does
: this, so you can simply write:
:
: myHash[key].swap( myset );

std::map has an operator[]:
Ty& operator[](const Key& keyval);
This function finds an existing map entry where .first==keyval.
If the element does not exist, a new entry is created where
..second is default-constructed.
A reference to the (new or pre-existing) .second member is returned.

You can modify the set provided by reference if appropriate.
If new contents are actually to be substituted, using the
..swap member function (provided by all standard containers)
allows you to exchange the contents of two containers without
any elements being copied.


hth -Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top