Boost logo

Boost Users :

Subject: [Boost-users] transform_iterator causing strange problems
From: Lindley M French (lfrench1_at_[hidden])
Date: 2009-05-26 19:09:59


I'm not sure if this is the fault of transform_iterator----I really don't see how it could be----but I'm really confused here.

I've got an unordered_map<GroupID, Group> (typedefed to GroupMap) in my class. The specifics of those types don't matter except that GroupID is an integral type, and Group contains a std::vector vec.

I'm trying to use a transform_iterator so that my class may be viewed as a group container:

typedef boost::transform_iterator<GroupSelector, GroupMap::const_iterator> const_iterator;
    const_iterator begin() const
    {
        return boost::make_transform_iterator(currgroups.begin(),GroupSelector());
    }
    const_iterator end() const
    {
        return boost::make_transform_iterator(currgroups.end(),GroupSelector());
    }
    std::pair<const_iterator, const_iterator> groups() const
    {
        const Group &grp = *(begin());
        return std::make_pair(begin(),end());
    }

Where my GroupSelector class is defined as

struct GroupSelector
{
    typedef const Group& result_type;
    const Group& operator()(const std::pair<GroupID, Group> &p) const
    {
        std::cout << p.second.vec.size() << " " << &p.second << std::endl;
        return p.second;
    }
};

Note the output statement. Now, the usage in my main function is

for (tie(iter,end) = final.groups(); iter != end; ++iter)
    {
        const Group& grp = *iter;
        cerr << &grp << endl;
        cerr << grp.segs.size() << endl;
...

My output is
10 0012F50C
10 0012F65C
0012F65C
1243176

which is strange for two reasons. The first line comes from GroupSelector when I dereference begin() in the groups() function. The second comes from GroupSelector when I dereference iter in main. As these should both correspond to the same iterator, I can't understand why the address of the returned Group reference is different.

The 3rd and 4th lines are outputted in main. Here's the second confusion: The address for the Group object is the same as in the most recent GroupSelector output, yet now the size of the vector within the Group comes out wrong. HUH?


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net