std::modulus is an STL functor to perform operator% in algorithms.
#include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <vector> int main() { //Create a std::vector with elements {0,1,2,3,4,5,6,7,8,9} std::vector<int> v; for (int i=0; i!=10; ++i) v.push_back(i); //Display the std::vector std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout," ")); std::cout << std::endl; //Perform a modulus of two on every element std::transform(v.begin(),v.end(),v.begin(),std::bind2nd(std::modulus<int>(),2)); //Display the std::vector std::copy(v.begin(),v.end(),std::ostream_iterator<int>(std::cout," ")); std::cout << std::endl; }
Screen output: