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

Discussion in 'C++' started by tezheng, Nov 27, 2006.

  1. tezheng

    tezheng Guest

    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.
     
    tezheng, Nov 27, 2006
    #1
    1. Advertising

  2. tezheng

    dasjotre Guest

    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.
     
    dasjotre, Nov 27, 2006
    #2
    1. Advertising

  3. tezheng

    David Harmon Guest

    On 27 Nov 2006 08:12:52 -0800 in comp.lang.c++, "dasjotre"
    <> wrote,
    >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());
     
    David Harmon, Nov 28, 2006
    #3
  4. tezheng

    dasjotre Guest

    David Harmon wrote:
    > On 27 Nov 2006 08:12:52 -0800 in comp.lang.c++, "dasjotre"
    > <> wrote,
    > >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
     
    dasjotre, Nov 28, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KK
    Replies:
    1
    Views:
    321
  2. Replies:
    3
    Views:
    401
    Pete Becker
    May 22, 2006
  3. Replies:
    1
    Views:
    451
    red floyd
    Dec 21, 2008
  4. Thomas J. Gritzan
    Replies:
    6
    Views:
    1,049
    James Kanze
    Dec 22, 2008
  5. James Kanze
    Replies:
    0
    Views:
    2,066
    James Kanze
    Dec 21, 2008
Loading...

Share This Page