how to use "set_difference" with std::map?

T

tezheng

since std::map is some kind of std::set<std::pair>
can i use "set_difference" with std::map?
how to deal with the "operator =" of std::pair<T1, T2>, as T1 and T2 could
be various type.
 
D

dasjotre

tezheng said:
since std::map is some kind of std::set<std::pair>
can i use "set_difference" with std::map?
how to deal with the "operator =" of std::pair<T1, T2>, as T1 and T2 could
be various type.

set_difference does not use equality
operator. You need to define less than
predicate. this predicate must define
the same ordering as map's key comparator.
iteration from map::begin() to map::end() will
list all elements in ascending order WRT
map's key comparator.

if you wan't to compare the keys only
you could use value_comp member
function from map.

so you can write:

std::set_difference(m1.begin(), m1.end()
, m2.begin(), m2.end(), some_output_iterator
, m1.value_comp());

set_difference stores input into
some_output_iterator. You would ether have to
write one yourself, or use back_inserter
on a list, or a vector, and then manually
insert those elements into your output
map.
 
D

David Harmon

On 27 Nov 2006 08:12:52 -0800 in comp.lang.c++, "dasjotre"
std::set_difference(m1.begin(), m1.end()
, m2.begin(), m2.end(), some_output_iterator
, m1.value_comp());

set_difference stores input into
some_output_iterator. You would ether have to
write one yourself, or use back_inserter
on a list, or a vector, and then manually
insert those elements into your output
map.

An excellent explanation, except what's wrong with?

std::set_difference(m1.begin(), m1.end(),
m2.begin(), m2.end(),
std::inserter(m3, m3.end()),
m1.value_comp());
 
D

dasjotre

David said:
On 27 Nov 2006 08:12:52 -0800 in comp.lang.c++, "dasjotre"


An excellent explanation, except what's wrong with?

std::set_difference(m1.begin(), m1.end(),
m2.begin(), m2.end(),
std::inserter(m3, m3.end()),
m1.value_comp());

verbosity
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top