std::modulus là function object (functor) trong header <functional> thực hiện phép chia lấy dư (tương đương toán tử %).
Cú pháp
template <class T> struct modulus : binary_function<T,T,T> {
T operator()(const T& a, const T& b) const { return a % b; }
};
Ví dụ
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
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 " << (rem[i]==0 ? "even" : "odd") << "\n";
return 0;
}
Đầu ra
10 is even 20 is even 35 is odd 45 is odd 50 is even