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

std::

bit_and

类模板  <functional>

template <class T> struct bit_and;

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

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

它的行为定义如下:
template <class T> struct bit_and {
  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_and example
#include <iostream>     // std::cout
#include <functional>   // std::bit_and
#include <algorithm>    // std::transform
#include <iterator>     // std::end


int main () {
  int values[] = {100,200,300,400,500};
  int masks[] = {0xf,0xf,0xf,255,255};
  int results[5];

  std::transform (values, std::end(values), masks, results, std::bit_and<int>());

  std::cout << "results:";
  for (const int& x: results)
    std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}

输出:
results: 4 8 12 144 244

🍄  另请参阅



bit_or 按位或函数对象类(类模板)
bit_xor 按位异或函数对象类(类模板)

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