c++中添加两个bool数组
c++ adding two bool arrays
我有一个数字作为bool数组,但我需要做算术运算,如加减和逻辑,如AND
与其他数字类似。如何在c++中完成这些操作,而不需要处理所有布尔特定的计算,并且做得很简单。示例:
bool a[10];
bool b[10];
bool c[10];
c = a + b;
您可以使用std::bitset
#include <bitset>
std::bitset<10> a(4);
std::bitset<10> b("0000001000");
std::bitset c = a.to_ulong() + b.to_ulong();
//Etc.
//You can also use
a[0] = 4; a[1] = 5; //to initialize / access
std::transform
可以对来自两个容器的元素对执行二进制操作,并将结果放入第三个容器。您可以使用std::logical_and
和std::logical_or
来获得您想要的结果:
transform(a, a+10,
b, b+10,
c, logical_and<bool>());
transform(a, a+10,
b, b+10,
c, logical_or<bool>());
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 两个字符串在 c++ 中不相等
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 两个文件使用彼此的功能-如何解决
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 防止在执行时将两个类中的两个对象转换为 bool:if(a!=b)
- 保留两个值相反的bool类成员
- 线程同步:等待两个bool变量
- 使用来自同一个bool c++的两个if语句的最佳实践
- 比较两个向量<bool>与SSE的关系
- 在c/c++中添加两个bool值
- 'bool operator<(Contact&)' 必须恰好取两个参数
- c++中添加两个bool数组