multimap, avoiding duplicated key->value pairs

H

He Shiming

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,
 
D

Donovan Hawkins

He Shiming said:
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
 
H

He Shiming

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::allocator<_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 said:
He Shiming said:
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
 
H

He Shiming

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

--
He Shiming

Donovan Hawkins said:
He Shiming said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,079
Latest member
ElidaWarin

Latest Threads

Top