Modulus function in C++ STL Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Modulus function is used to return the value of the modulus between its two arguments. It works same as modulus operator works. template struct modulus : binary_function { T operator() (const T& x, const T& y) const { return x%y; } }; Member types: Type of first argument Type of second argument Type of result returned by member operator Note: We must include library 'functional' and 'algorithm' to use modulus and transform. Bewlo programs illustrate the working of modulus function: CPP // C++ program to implement modulus function #include <algorithm> // transform #include <functional> // modulus, bind2nd #include <iostream> // cout using namespace std; int main() { // defining the array int array[] = { 8, 6, 3, 4, 1 }; int remainders[5]; // transform function that helps to apply // modulus between the arguments transform(array, array + 5, remainders, bind2nd(modulus<int>(), 2)); for (int i = 0; i < 5; i++) // printing the results while checking // whether no. is even or odd cout << array[i] << " is a " << (remainders[i] == 0 ? "even" : "odd") << endl; return 0; } Output: 8 is a even 6 is a even 3 is a odd 4 is a even 1 is a odd CPP // C++ program to implement modulus function #include <algorithm> // transform #include <functional> // modulus, bind2nd #include <iostream> // cout #include <iterator> #include <vector> using namespace std; int main() { // Create a std::vector with elements // {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} vector<int> v; for (int i = 0; i < 10; ++i) v.push_back(i); // Perform a modulus of two on every element transform(v.begin(), v.end(), v.begin(), bind2nd(modulus<int>(), 2)); // Display the vector copy(v.begin(), v.end(), ostream_iterator<int>(cout, " ")); cout << endl; return 0; } Output: 0 1 0 1 0 1 0 1 0 1 Comment More info S shubham tyagi 4 Follow Improve Article Tags : Misc Technical Scripter C++ STL CPP-Functions cpp-template +2 More Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++5 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++11 min readFile Handling through C++ Classes8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like