Home C&C++函数库 c++ 语法 程序源码 Linux C库

std::

modulus

类模板  <functional>

template <class T> struct modulus;

模函数对象类
二元函数对象类,其调用将返回两个实参之间求模操作的结果(由运算符%返回)。

一般来说,函数对象是定义了成员函数operator()的类的实例。该成员函数允许使用与函数调用相同的语法来使用对象。

它的行为定义如下:

C++98
template <class T> struct modulus : binary_function <T,T,T> {
  T operator() (const T& x, const T& y) const {return x%y;}
};


该类的对象可用于标准算法,如transform 和 accumulate。

☲  模板参数


T
参数的类型和函数调用的返回类型。
类型应支持操作(operator%)。

☲  成员类型


成员类型 定义 注释
first_argument_type T 成员operator()中第一个参数的类型
second_argument_type T 成员operator()中第二个参数的类型
result_type T 成员operator()返回的类型

☲  成员函数



T operator() (const T& x, const T& y)
成员函数返回参数之间的模运算(x%y)。

☣  示例



// modulus example
#include <iostream>     // std::cout
#include <functional>   // std::modulus, std::bind2nd
#include <algorithm>    // std::transform

int main () {
  int numbers[]={1,2,3,4,5};
  int remainders[5];
  std::transform (numbers, numbers+5, remainders, std::bind2nd(std::modulus<int>(),2));
  for (int i=0; i<5; i++)
    std::cout << numbers[i] << " is " << (remainders[i]==0?"even":"odd") << '\n';
  return 0;
}

输出:
1 is odd
2 is even
3 is odd
4 is even
5 is odd

🍄  另请参阅



plus 加法函数对象类(类模板)
minus 减法函数对象类(类模板)
multiplies 乘法函数对象类(类模板)
divides 除法函数对象类(类模板)
negate 取反函数对象类(类模板)
equal_to 用于相等比较的函数对象类(类模板)

联系我们 免责声明 关于CandCplus 网站地图