C++ – STL way to add a constant value to a std::vector

cstl

Is there an algorithm in the standard library that can add a value to each element of a std::vector?
Something like

std::vector<double> myvec(5,0.);
std::add_constant(myvec.begin(), myvec.end(), 1.);

that adds the value 1.0 to each element?

If there isn't a nice (e.g. short, beautiful, easy to read) way to do this in STL, how about boost?

Best Answer

Even shorter using lambda functions, if you use C++0x:

std::for_each(myvec.begin(), myvec.end(), [](double& d) { d+=1.0;});