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

std::

bit_xor

类模板  <functional>

template <class T> struct bit_xor;

按位异或函数对象类
二元函数对象类,其调用将返回在两个实参之间应用按位“异或”操作的结果(与operator ^ 返回的结果相同)。

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

它的行为定义如下:
template <class T> struct bit_xor {
  T operator() (const T& x, const T& y) const {return x^y;}
  typedef T first_argument_type;
  typedef T second_argument_type;
  typedef T result_type;
};

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

☲  模板参数


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

☲  成员类型


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

☲  成员函数



T operator() (const T& x, const T& y)

成员函数返回其参数(x^y)的按位“异或”。

☣  示例



// bit_xor example
#include <iostream>     // std::cout
#include <functional>   // std::bit_xor
#include <algorithm>    // std::accumulate
#include <iterator>     // std::end

int main () {
  int flags[] = {1,2,3,4,5,6,7,8,9,10};
  int acc = std::accumulate (flags, std::end(flags), 0, std::bit_xor<int>());
  std::cout << "xor: " << acc << '\n';
  return 0;
}

输出:
xor: 11

🍄  另请参阅



bit_and 按位与函数对象类(类模板)
bit_or 按位或函数对象类(类模板)

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