473,509 Members | 2,912 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,int> 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 6278
"He Shiming" <mailbill(NOSPAM)@21cn.com.nospam> wrote in message
news:33*************@individual.net...
Happy holidays! I have a question regarding STL multimap. Basically, the
current multimap<int,int> 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\include\utility(41) :
error C2664: 'std::set<_Kty>::set(const std::set<_Kty>::key_compare &)' :
cannot convert parameter 1 from 'const std::allocator<_Ty>::value_type' to
'const std::set<_Kty>::key_compare &'
with
[
_Kty=std::wstring
]
and
[
_Ty=std::wstring
]
and
[
_Kty=std::wstring
]
Reason: cannot convert from 'const std::allocator<_Ty>::value_type'
to 'const std::set<_Kty>::key_compare'
with
[
_Ty=std::wstring
]
and
[
_Kty=std::wstring
]
No constructor could take the source type, or constructor overload
resolution was ambiguous
f:\-= Projects =-\-= Programming
=-\MediaMan\MediaMan\Framework\System\RegKeyEx.h(93) : see reference to
function template instantiation
'std::pair<_Ty1,_Ty2>::pair<std::wstring,std::allo cator<_Ty>::value_type>(const
std::pair<std::wstring,std::allocator<_Ty>::value_ type> &)' being compiled
with
[
_Ty1=const std::wstring,
_Ty2=std::set<std::wstring>,
_Ty=std::wstring
]

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.edu> wrote in message
news:it******************@twister.socal.rr.com...
"He Shiming" <mailbill(NOSPAM)@21cn.com.nospam> wrote in message
news:33*************@individual.net...
Happy holidays! I have a question regarding STL multimap. Basically, the
current multimap<int,int> 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.edu> wrote in message
news:it******************@twister.socal.rr.com...
"He Shiming" <mailbill(NOSPAM)@21cn.com.nospam> wrote in message
news:33*************@individual.net...
Happy holidays! I have a question regarding STL multimap. Basically, the
current multimap<int,int> 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
4706
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
4157
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...
3
7576
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...
1
1466
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...
13
2334
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...
5
4220
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...
3
2814
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...
6
9845
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...
2
2165
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
1611
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...
0
7234
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
7412
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...
0
7505
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...
0
5652
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
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...
0
3216
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...
0
1570
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 ...
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
441
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.