473,216 Members | 2,186 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,216 software developers and data experts.

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 6227
"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
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
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
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
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
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
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
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
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
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
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
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.