You're not linking against the standard C++ library. Make sure you call g++, not gcc, to compile the code.
A free function in this case is not so good - it might not get inlined, for one. Better would be a function object.
Code:
template<typename T>
struct increment : std::unary_function<void, T&>
{
void operator(T& t) { ++t; }
};
...
std::for_each(v.begin(), v.end(), increment<int>());
Or you could use std::transform, std::bind1st and std:lus:
Code:
std::transform(v.begin(), v.end(), v.begin(), std::bind1st(std::plus<int>(), 1));
Or you could use Boost.Bind.
Code:
std::transform(v.begin(), v.end(), v.begin(), boost::bind(std::plus<int>(), _1, 1));
Or Boost.Lambda.
Code:
namespace ll = boost::lambda;
std::transform(v.begin(), v.end(), v.begin(), ll::_1 + 1);
Or again Boost.Lambda, but with for_each.
Code:
std::for_each(v.begin(), v.end(), ++ll::_1);
Oh, the possibilities.
By the way, with C++0x lambdas, the same code will be
Code:
std::for_each(v.begin(), v.end(), [](int &i) { ++i; });