📜  C++中的std :: bit_or以及示例

📅  最后修改于: 2021-04-28 17:47:34             🧑  作者: Mango

bit_or是C++中的内置函数,用于执行bitwise_or并在对参数执行bitwise_or操作后返回结果。

头文件:

#include 

模板类别:

template  struct bit_or;

参数:它接受参数T ,该参数T是要由功能调用进行比较的参数的类型。

笔记:

  1. 此类的对象可用于标准算法,例如变换或累加。
  2. 成员函数(“运算符()”)返回其参数的bitwise_or。

我们必须包含库“ functional”“ algorithm”以分别使用bit_or和transform()。

下面是C++中bit_or的图示:

问题一:

// C++ program to show the
// functionality of bit_or
  
#include  // to include transform
#include  // to include bit_or
#include 
#include 
using namespace std;
  
int main()
{
    // declaring the values
    int xyz[] = { -7, 2, 5, 100, 1029 };
    int abc[] = { -4, 0, 5, 1, 1 };
    int n = 5;
    // defining results
    int results[n];
  
    // transform is used to apply
    // bitwise_or on the arguments
    transform(xyz, end(xyz), abc,
              results, bit_or());
  
    // printing the resulting array
    cout << "Results:";
    for (const int& x : results)
        cout << ' ' << x;
  
    return 0;
}
输出:
Results: -3 2 5 101 1029

问题2:

// C++ program to show the
// functionality of bit_or
  
#include 
#include 
#include 
#include 
using namespace std;
  
int main()
{
    // declaring the values
    // in form of hexadecimals
    int xyz[] = { 0, 1100 };
    int abc[] = { 0xf, 0xf };
  
    // defining results
    int results[2];
  
    // transform is used to apply
    // bitwise_or on the arguments
    transform(xyz, end(xyz), abc,
              results, bit_or());
  
    // printing the resulting array
    cout << "Results:";
    for (const int& x : results)
        cout << ' ' << x;
  
    return 0;
}
输出:
Results: 15 1103

参考: https //en.cppreference.com/w/cpp/utility/functional/bit_or

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”