Skip to content

Latest commit

 

History

History
48 lines (27 loc) · 2.36 KB

CppStdModulus.md

File metadata and controls

48 lines (27 loc) · 2.36 KB

 

 

 

 

 

 

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:

 


0 1 2 3 4 5 6 7 8 9 0 1 0 1 0 1 0 1 0 1