applying partial_sum to a map container

Discussion in 'C++' started by cesco, Sep 11, 2006.

  1. cesco

    cesco Guest

    Hi,

    I have a map container where the key_type is a pointer to a class and
    the value_type is an intenger. I'd like to apply the STL algorithm
    partial_sum to the value_type field of that map but I don't know how to
    do it. Probably I have to specify a functor as fourth argument of
    partial_sum but I don't know how to implement that.
    Can anyone help?

    Thanks and regards
    Cesco
    cesco, Sep 11, 2006
    #1
    1. Advertising

  2. cesco

    Nate Barney Guest

    cesco wrote:
    >
    > I have a map container where the key_type is a pointer to a class


    If you use a pointer as a key type to an associateive container, you
    need to make sure you provide a comparison that's based on the
    referent of the pointer. Comparing pointers based on their actual
    values is only meaningful when they are pointers into the same
    object/array.

    > and the value_type is an intenger.


    I think you mean data_type is an integer. value_type is actually a
    std::pair<const key_type,data_type>

    > I'd like to apply the STL algorithm partial_sum to the value_type field of
    > that map but I don't know how to do it. Probably I have to specify a functor
    > as fourth argument of partial_sum but I don't know how to implement that.


    I thought functor at first, too, but that won't work. The initial
    value of the sum is set to the value at the beginning of
    the input range, which, for a map, is a std::pair.

    What you can do, though, is write a wrapper iterator that
    returns only the second part of each pair:

    #include <iterator>

    template <typename It>
    class map_value_iterator : public
    std::iterator<std::input_iterator_tag,
    typename std::iterator_traits<It>::value_type::second_type>
    {
    public:

    map_value_iterator(It it) : it(it) {}
    map_value_iterator(const map_value_iterator<It> &mvi)
    : it(mvi.it) {}

    map_value_iterator &operator++() { ++it; return *this; }
    map_value_iterator operator++(int) { It i = it; ++it; return i; }

    bool operator==(const map_value_iterator<It> &mvi) const
    { return it == mvi.it; }
    bool operator!=(const map_value_iterator<It> &mvi) const
    { return it != mvi.it; }
    bool operator==(const It &i) const { return it == i; }
    bool operator!=(const It &i) const { return it != i; }

    const typename std::iterator_traits<It>::value_type::second_type *
    operator->() const { return &it->second; }
    const typename std::iterator_traits<It>::value_type::second_type &
    operator*() const { return it->second; }

    private:

    It it;
    };

    You could then use it like this:

    typedef std::map<int,int> IntMap;
    typedef std::vector<int> IntVec;

    IntMap m;
    IntVec v;

    m[0] = 1;
    m[1] = 2;
    m[2] = 4;

    map_value_iterator<IntMap::iterator> begin = m.begin();
    map_value_iterator<IntMap::iterator> end = m.end();

    std::partial_sum(begin,end,back_inserter(v));
    Nate Barney, Sep 11, 2006
    #2
    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. Ken Cox [Microsoft MVP]
    Replies:
    1
    Views:
    3,446
    Charlie@CBFC
    May 12, 2004
  2. Vivi Orunitia
    Replies:
    11
    Views:
    4,448
    Martijn Lievaart
    Feb 4, 2004
  3. Maitre Bart
    Replies:
    2
    Views:
    510
    Maitre Bart
    Feb 11, 2004
  4. puzzlecracker
    Replies:
    8
    Views:
    288
  5. Michael DOUBEZ
    Replies:
    0
    Views:
    454
    Michael DOUBEZ
    Nov 27, 2008
Loading...

Share This Page