469,140 Members | 1,164 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,140 developers. It's quick & easy.

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 5501
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Markus Dehmann | last post: by
3 posts views Thread by Stan McCann | last post: by
6 posts views Thread by Evyn | last post: by
2 posts views Thread by Sehboo | last post: by
reply views Thread by =?Utf-8?B?SGVucnlDQw==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.