Modulus function in C++ STL Last Updated : 14 Sep, 2018 Summarize Comments Improve Suggest changes Share 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 infoAdvertise with us Next Article mktime() function in C++ STL S shubham tyagi 4 Follow Improve Article Tags : Misc Technical Scripter C++ STL CPP-Functions cpp-template +2 More Practice Tags : CPPMiscSTL Similar Reads logb() function in C++ STL The logb() is a builtin function in C++ STL which returns the logarithm of |x|, using FLT_RADIX as base for the logarithm. In general, the value of FLT_RADIX is 2, so logb() is equivalent to log2()(for positive values only). Syntax: logb(val) Parameter: The function accepts a single mandatory parame 2 min read lldiv() function in C++ STL The lldiv() is a builtin function in C++ STL which gives us the quotient and remainder of the division of two numbers. Syntax: lldiv(n, d) Parameters: The function accepts two mandatory parameters which are described below: n: It specifies the dividend. The data-type can be long long or long long in 2 min read strtol() function in C++ STL The strtol() function is a builtin function in C++ STL which converts the contents of a string as an integral number of the specified base and return its value as a long int. Syntax: strtol(s, &end, b) Parameters: The function accepts three mandatory parameters which are described as below: s: s 4 min read mktime() function in C++ STL The mktime() is an inbuilt C++ function which converts the local calendar time to the time since epoch and returns the value as an object of type time_t. Syntax : time_t mktime( struct tm *time_ptr ) Parameters: The function accepts a mandatory parameter pointer time_ptr that points to a tm object s 2 min read list end() function in C++ STL The list::end() is a built-in function in C++ STL which is used to get an iterator to past the last element. By past the last element it is meant that the iterator returned by the end() function return an iterator to an element which follows the last element in the list container. It can not be used 2 min read map rend() function in C++ STL The rend() function is an inbuilt function in C++ STL which returns a reverse iterator pointing to the theoretical element right before the first key-value pair in the map(which is considered its reverse end). Syntax: map_name.rend() Parameters:The function does not take any parameter. Return Value: 2 min read Like