Re: [Boost-users] reference access with boost::make_transform_iterator
Hi,
I'm trying to use boost::make_transform_iterator to create an iterator for a
custom class whose data is held in a map and where the iterator uses the
vector of keys to access the values.
In my problem, the values of the map are containers which hold large data.
Since I can't afford to copy the data, I would like to access the data by
reference via the iterator. However, when doing this,
the data is corrupted, as is exemplified by the output of the simple
example I have attached. As far as I can tell the problem lies in the use
of the from_key functor, which is initialised using a reference to the map
and is corrupted when passed through make_transform_iterator.
Any ideas how I could do this properly using boost?
Thanks,
Patrick
#include <iostream>
#include <string>
#include <vector>
#include
Hi Patrick,
On Mon, Jan 24, 2011 at 3:55 PM, Patrick Sauer
In my problem, the values of the map are containers which hold large data. Since I can't afford to copy the data, I would like to access the data by reference via the iterator. However, when doing this, the data is corrupted, as is exemplified by the output of the simple example I have attached.
The problem isn't actually with transform_iterator -- it's the result_type of from_key. The transform_iterator result used a copy of the internal holder object. Since the holder::v is not a reference, accessing it from the copy of holder resulted in a dangling reference. The solution is to change the typedef below:
class from_key { public:
typedef holder result_type; ^^ Change to: typedef holder const& result_type;
HTH, Nate
participants (2)
-
Nathan Crookston
-
Patrick Sauer