
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Modulus function in C++ STL
In this article, we will be discussing the working, syntax, and examples of modulus functions in C++.
What is modulus function C++?
modulus function object class in C++, which is defined in <functional> header file. modulus function is a binary function object class used to get the result of the modulus operation of the two arguments. This function works the same as the operator ‘% ‘.
Syntax of modulus function
Template struct modulus : binary_function { T operator() (const T& a, const T& b) const {return a%b; } };
Template parameters
The function accepts the following parameter(s) −
T − This is the type of the argument passed to function call.
Example
#include <iostream> #include <algorithm> #include <functional&g; using namespace std; int main(){ //create an array int arr[] = { 10, 20, 35, 45, 50, 61 }; int rem[6]; transform(arr, arr + 6, rem,bind2nd(modulus<int>(), 2)); for (int i = 0; i < 5; i++){ cout << arr[i] << " is a "<<(rem[i] == 0 ? "even" : "odd")<<"\n"; } return 0; }
Output
If we run the above code it will generate the following output −
10 is a even 20 is a even 35 is a odd 45 is a odd 50 is a even
- Related Articles
- sinh() function in C++ STL
- cosh() function in C++ STL
- atanh() function in C++ STL
- tanh() function in C++ STL
- acosh() function in C++ STL
- asinh() function in C++ STL
- acos() function in C++ STL
- atan2() function in C++ STL
- iswalnum() function in C++ STL
- iswalpha() function in C++ STL
- lldiv() function in C++ STL
- negate function in C++ STL
- iswblank() function in C++ STL
- iswcntrl() function in C++ STL
- iswdigit() function in C++ STL

Advertisements