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

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

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.
Nov 27 '06 #1
3 8703
tezheng wrote:
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.

Nov 27 '06 #2
On 27 Nov 2006 08:12:52 -0800 in comp.lang.c++, "dasjotre"
<da******@googlemail.comwrote,
>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());

Nov 28 '06 #3

David Harmon wrote:
On 27 Nov 2006 08:12:52 -0800 in comp.lang.c++, "dasjotre"
<da******@googlemail.comwrote,
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());
verbosity

Nov 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

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.