473,569 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multimap, avoiding duplicated key->value pairs

Hi Folks,

Happy holidays! I have a question regarding STL multimap. Basically, the
current multimap<int,in t> look like this:

key=>value
1=>10,
1=>20,
1=>30,

When I'm inserting new key value pairs, I need to ensure that those pairs
already in the multimap can't be inserted again. Say that 1=>40 is okay, but
1=>10 is not okay. So that I get unique key->value pairs in the multimap.

I know that upon each insertion, I can do a check by iterating through keys
and values. But is there any easier or hopefully faster way to do this?

Thanks in advance,
--
He Shiming
Jul 22 '05 #1
3 6288
"He Shiming" <mailbill(NOSPA M)@21cn.com.nos pam> wrote in message
news:33******** *****@individua l.net...
Happy holidays! I have a question regarding STL multimap. Basically, the
current multimap<int,in t> look like this:

key=>value
1=>10,
1=>20,
1=>30,

When I'm inserting new key value pairs, I need to ensure that those pairs
already in the multimap can't be inserted again. Say that 1=>40 is okay, but 1=>10 is not okay. So that I get unique key->value pairs in the multimap.


How about using a map< int, set<int> > instead?

std::map< int, std::set<int> > m;
m[1].insert(10);
m[1].insert(20);
m[1].insert(30);
m[1].insert(40);
m[1].insert(10);
assert( m[1].size() == 4 );

Donovan Hawkins
Jul 22 '05 #2
Hi Donovan,

Thank you for your answer. As I'm trying to use set<> on std::wstring (not
"int" in my actual case), I get a lot of error messages when compiling, like
this:

e:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\includ e\utility(41) :
error C2664: 'std::set<_Kty> ::set(const std::set<_Kty>: :key_compare &)' :
cannot convert parameter 1 from 'const std::allocator< _Ty>::value_typ e' to
'const std::set<_Kty>: :key_compare &'
with
[
_Kty=std::wstri ng
]
and
[
_Ty=std::wstrin g
]
and
[
_Kty=std::wstri ng
]
Reason: cannot convert from 'const std::allocator< _Ty>::value_typ e'
to 'const std::set<_Kty>: :key_compare'
with
[
_Ty=std::wstrin g
]
and
[
_Kty=std::wstri ng
]
No constructor could take the source type, or constructor overload
resolution was ambiguous
f:\-= Projects =-\-= Programming
=-\MediaMan\Media Man\Framework\S ystem\RegKeyEx. h(93) : see reference to
function template instantiation
'std::pair<_Ty1 ,_Ty2>::pair<st d::wstring,std: :allocator<_Ty> ::value_type>(c onst
std::pair<std:: wstring,std::al locator<_Ty>::v alue_type> &)' being compiled
with
[
_Ty1=const std::wstring,
_Ty2=std::set<s td::wstring>,
_Ty=std::wstrin g
]

I'm wondering, as I'm not familiar with std::set, can it be used with
std::wstring, std::string or other objects?

Thanks,
--
He Shiming

"Donovan Hawkins" <ha*****@uci.ed u> wrote in message
news:it******** **********@twis ter.socal.rr.co m...
"He Shiming" <mailbill(NOSPA M)@21cn.com.nos pam> wrote in message
news:33******** *****@individua l.net...
Happy holidays! I have a question regarding STL multimap. Basically, the
current multimap<int,in t> look like this:

key=>value
1=>10,
1=>20,
1=>30,

When I'm inserting new key value pairs, I need to ensure that those pairs
already in the multimap can't be inserted again. Say that 1=>40 is okay,

but
1=>10 is not okay. So that I get unique key->value pairs in the multimap.


How about using a map< int, set<int> > instead?

std::map< int, std::set<int> > m;
m[1].insert(10);
m[1].insert(20);
m[1].insert(30);
m[1].insert(40);
m[1].insert(10);
assert( m[1].size() == 4 );

Donovan Hawkins

Jul 22 '05 #3
I'm sorry I got mixed up. I've worked it out now. Thank you very much for
your help.

--
He Shiming

"Donovan Hawkins" <ha*****@uci.ed u> wrote in message
news:it******** **********@twis ter.socal.rr.co m...
"He Shiming" <mailbill(NOSPA M)@21cn.com.nos pam> wrote in message
news:33******** *****@individua l.net...
Happy holidays! I have a question regarding STL multimap. Basically, the
current multimap<int,in t> look like this:

key=>value
1=>10,
1=>20,
1=>30,

When I'm inserting new key value pairs, I need to ensure that those pairs
already in the multimap can't be inserted again. Say that 1=>40 is okay,

but
1=>10 is not okay. So that I get unique key->value pairs in the multimap.


How about using a map< int, set<int> > instead?

std::map< int, std::set<int> > m;
m[1].insert(10);
m[1].insert(20);
m[1].insert(30);
m[1].insert(40);
m[1].insert(10);
assert( m[1].size() == 4 );

Donovan Hawkins

Jul 22 '05 #4

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

Similar topics

6
4708
by: Equis Uno | last post by:
Hi, Assume I'm given a 100k file full of key-value pairs: date,value 26-Feb-04,36.47 25-Feb-04,36.43 24-Feb-04,36.30 23-Feb-04,37.00 20-Feb-04,37.00
3
4163
by: Warp | last post by:
If I have a cookie with 'key/value' pairs, how would I delete a specific 'key/value' pair while preserving the remaining pairs? I tried to do something like: cookie(key)="" but it didn't seem to work. I do not want to expire the cookie, just to delete the specified key. I'm working in javascript. Thanks in advance.
3
7579
by: Jeff L. | last post by:
I have an interesting problem and I'm not coming up with any answers in my searches, so hopefully someone can give me a hand with this. I have a feeling it's easy, but I usually get my nose stuck too far in the details to see the big picture. :) I want to create a collection of key/value pairs. I have a class for the key/value...
1
1471
by: Hal Heinrich | last post by:
Hi, I have a hastable with key/value pairs. I want to iterate thru the collection in descending order by value. What is the 'best practice' for handling this problem? Thanks in advance for your help, Hal Heinrich VP Technology Aralan Solutions Inc.
13
2338
by: Markus Dehmann | last post by:
I have key-value pairs where the key consists of 3 integers, the value is a double. I am wondering what's the best way to store them (preferrably in an STL container). A fast lookup should be possible. The integers are not consecutive, but very sparse, so a 3-dim array is not s solution. I tried this: #include <iostream> #include <map>
5
4224
by: Simon | last post by:
Hi all, I am writing a windows application using vb.net on the 1.1 framework. We have in the application, some strongly typed collections that have been written as classes that do not inherit from collection base, but use an internal collection, to hold the objects and then implement IEnumerator, see example below,
3
2818
by: Stan McCann | last post by:
I've searched and searched for a function to create an array from a string maintaining key/value pairs and keep coming up blank. This seems to me that it would be quite commonly used. What I am doing is trying to pass an array using $_GET. To pass the array, it is rather simple to add it to my get string breaking the array into...
6
9851
by: Evyn | last post by:
Hi, How do I compare 2 maps for identical keys, and if they have identical keys, check if they have identical values? In either case I want to copy ONLY that key value pair to one of two different maps. I know how to copy the entire map to another: // Copy fset1 to fset3 std::copy(fset1.begin(),fset1.end(),std::inserter(fset3,...
2
2168
by: Sehboo | last post by:
I am trying to use key value pair list, but I don't want to use sorted list because it messes up my order. I am not sure what other options I have. Can anybody point? Thanks
0
1614
by: =?Utf-8?B?SGVucnlDQw==?= | last post by:
We are trying to develop a set of C# 2.0 web services that are data driven, loosely coupled, easy to maintain, and somewhat high preferment (I'm sure that's either an oxymoron or everyone else's dream too?!?) Anyway, we need to be able to take an "arbitrary" number of key (or name) /value pairs that do have a finite, but changeable, set of...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7982
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.