c++ - Iterator "pointing" to a member of an object -


i admit had difficulties coming reasonable description this. cannot think of term describe precisely i'm looking for. perhaps called slicing iterator.

let's have this:

struct s {     int i;     char *s;     float f; };  std::vector<s> v(10); 

what i'm looking way construct iterator, point member of s. i'd able pass std::min_element without creating predicate in each case. might this:

std::min_element(slicing_iterator(v.begin(), s::f), slicing_iterator(v.end(), s::f)); 

is there template trick use achieve this? or perhaps it's done somewhere in boost or other library?

if you're looking iterator converts s s::f, done using boost (what can't be?):

std::cout << *std::min_element(                boost::make_transform_iterator(v.begin(), boost::bind(&s::f, _1)),                boost::make_transform_iterator(v.end(), boost::bind(&s::f, _1))               ) << '\n'; 

test: https://ideone.com/jgchr

but if you're looking s s::f smallest in vector, predicate reasonable approach.


Comments