Multiply vector elements by a scalar value using STL

Yes, using std::transform:

std::transform(myv1.begin(), myv1.end(), myv1.begin(),
               std::bind(std::multiplies<T>(), std::placeholders::_1, 3));

Before C++17 you could use std::bind1st(), which was deprecated in C++11.

std::transform(myv1.begin(), myv1.end(), myv1.begin(),
               std::bind1st(std::multiplies<T>(), 3));

For the placeholders;

#include <functional> 

Leave a Comment