Finding max_element of a vector where a member is used to decide if its the maximum
问题 Consider a class A having a member x and a std::vector< A >. Now its a common task to search for the maximal x among all elements inside the vector. Clearly I can only use std::max_element if there is an iterator on the x's. But I must write one by my own, or I just make a simple for loop. maxSoFar = -std::numeric_limits< double >::max(); for( std::vector< A >::const_iterator cit = as.begin(); cit != as.end(); ++cit ) { if( cit->x > maxSoFar ) maxSoFar = cit->x; } but it's so tedious, and I