What’s the best way to count all elements in an iterator?
I want code equivalent to this
template<typename T,typename S,S val>
struct ConstantFunctor : unary_function<T,S>
{S operator()(const T&) const {return val;}};
template<typename T>
struct TrueFunctor : ConstantFunctor<T,bool,true>{};
...
count_if(c.begin(),c.end(),TrueFunctor());
What’s the best way to do that?
I can use boost::lambda::constant(true), but maybe there’s something clearer.
If you want to count all elements in a range. then you can use
std::distance, from the<iterator>header, like so:It should be enough.
The online doc says about
std::distance: