std::for_each isn't designed for that. Use std::count to count the number of elements which is equal to some value, or std::count_if to count the elements which satisfies some predicate:
std::vector<SomeType> vec;
std::count(vec.begin(), vec.end(), SomeType(9));
/*or */
bool myfunc(const SomeType& v)
{
return v == 9;
}
std::count_if(vec.begin(), vec.end(), f);
If you just want to copy the contents of a container to an ostream object like std::cout, use std::copy instead:
std::vector<SomeType> vec;
...
std::copy(vec.begin(), vec.end(), \
std::ostream_iterator<SomeType>(std::cout," "));
If you need the return value from each invocation of the function, use std::transform:
std::vector<SomeType> src;
std::vector<SomeType> result;
int myfunc(int val)
{
...
}
std::transform(src.begin(), src.end() \
result.begin(), myfunc);
std::transform is also overloaded so it works for binary functions as well as unary functions.