473,387 Members | 1,516 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,387 software developers and data experts.

set inside a map

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

May 25 '06 #1
6 3535
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?

May 25 '06 #2

"Milind" <ma******@gmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
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.
May 25 '06 #3
Very useful suggestion indeed.

+1 from my side :) Thnx.

~M

May 25 '06 #4
"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:pq***************@fe03.lga...
:
: "Milind" <ma******@gmail.com> wrote in message
: news:11**********************@i39g2000cwa.googlegr oups.com...
: > 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
May 25 '06 #5
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

May 25 '06 #6
"Milind" <ma******@gmail.com> wrote in message
news:11*********************@38g2000cwa.googlegrou ps.com...
: 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
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
May 25 '06 #7

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

Similar topics

0
by: Nick Coghlan | last post by:
Anyone playing with the CPython interpreter's new command line switch might have noticed that it only works with top-level modules (i.e. scripts that are directly on sys.path). If the script is...
15
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that...
9
by: sathya | last post by:
I was going through an Boyer-Moore-Horspool pattern match, I saw a array inside a array, like the below, skip ] = patlen - i - 1; The array is decleared as int skip; unsigned char *pat;
0
by: Severino | last post by:
Hi all, we have developed a .NET component for use inside Windows Forms: this component has been written using VC++.NET (2003) and is working perfectly when inserted inside VC#.NET or VB.NET...
0
by: Severino | last post by:
Hi all, we have developed a .NET component for use inside Windows Forms: this component has been written using VC++.NET (2003) and is working perfectly when inserted inside VC#.NET or VB.NET...
0
by: Les Caudle | last post by:
I have a menu system composed of a DataList nested inside a DataList. The outer DataList has it's DataSource (composed of a DataSet with two tables linked by a CategoryPagesRelation Relation) set...
1
by: Matik | last post by:
Hi to all, Probably I'm just doing something stupid, but I would like you to tell me that (if it is so), and point the solution. There ist the thing: I' having a sp, where I call other sp...
4
by: Rares Vernica | last post by:
Hi, How can I save a reference inside a container? For example I have: map<string, unsignedX; I would like to be able to save a reference to a position inside X. For a vector, the...
5
by: Test | last post by:
Is it possibel to have DIV's inside a table cell so that their position can be given relative to top left corner of the cell. Now the DIVs seem to position themselves relative to previous object.
14
by: raylopez99 | last post by:
KeyDown won't work KeyPress fails KeyDown not seen inspired by a poster here:http://tinyurl.com/62d97l I found some interesting stuff, which I reproduce below for newbies like me. The main...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.