applying partial_sum to a map container

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cesco

    #1

    applying partial_sum to a map container

    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

  • Nate Barney

    #2
    Re: applying partial_sum to a map container

    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_t ype>
    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_itera tor : public
    std::iterator<s td::input_itera tor_tag,
    typename std::iterator_t raits<It>::valu e_type::second_ type>
    {
    public:

    map_value_itera tor(It it) : it(it) {}
    map_value_itera tor(const map_value_itera tor<It&mvi)
    : it(mvi.it) {}

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

    bool operator==(cons t map_value_itera tor<It&mvi) const
    { return it == mvi.it; }
    bool operator!=(cons t map_value_itera tor<It&mvi) const
    { return it != mvi.it; }
    bool operator==(cons t It &i) const { return it == i; }
    bool operator!=(cons t It &i) const { return it != i; }

    const typename std::iterator_t raits<It>::valu e_type::second_ type *
    operator->() const { return &it->second; }
    const typename std::iterator_t raits<It>::valu e_type::second_ type &
    operator*() const { return it->second; }

    private:

    It it;
    };

    You could then use it like this:

    typedef std::map<int,in tIntMap;
    typedef std::vector<int IntVec;

    IntMap m;
    IntVec v;

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

    map_value_itera tor<IntMap::ite ratorbegin = m.begin();
    map_value_itera tor<IntMap::ite ratorend = m.end();

    std::partial_su m(begin,end,bac k_inserter(v));

    Comment

    Working...