Boost logo

Boost :

Subject: [boost] [range] Adapter to select class member
From: Mark Snelling (mark_at_[hidden])
Date: 2012-12-17 11:29:30


Hi,
I'm not sure if this has already been done but I have created a new
Boost.Range adapter that will return a specified member variable of the
class in the range, similar to the existing map_keys and map_values
adapters.
Would there be interest in this?

For example consider the following code:

#include <boost/range/adaptor/member.hpp>
#include <boost/range/algorithm/copy.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <vector>
#include <string>

struct A
{
int foo;
 std::string bar;
};

int main(int argc, char* argv[])
{
 std::vector< A > input;
for ( int i = 0; i < 10; ++i )
 {
 A a;
a.foo = i;
 a.bar = boost::lexical_cast< std::string >( i * 10 );
 input.push_back( a );
 }

 boost::copy(
input | boost::adaptors::selected_member( &A::foo ),
 std::ostream_iterator< int >( std::cout, "," ) );

std::cout << std::endl;

boost::copy(
 input | boost::adaptors::selected_member( &A::bar ),
 std::ostream_iterator< std::string >( std::cout, "," ) );

return 0;
}

Cheers,

Mark


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk