C++ 数组中所有元素之间的逻辑操作
我希望使用布尔运算符以最有效的方式比较数组中的所有项。目前,我有这样的想法:C++ 数组中所有元素之间的逻辑操作,c++,arrays,operators,bitwise-operators,C++,Arrays,Operators,Bitwise Operators,我希望使用布尔运算符以最有效的方式比较数组中的所有项。目前,我有这样的想法: bool myFunction(int i) { bool *myArray = {true, true, false, false, true}; //suppose we know that i = 5 and that i is the length of the array... return myArray[0] && myArray[1] && m
bool myFunction(int i)
{
bool *myArray = {true, true, false, false, true};
//suppose we know that i = 5 and that i is the length of the array...
return myArray[0] && myArray[1] && myArray[2] && myArray[3] && myArray[4];
}
由于数组的大小不是固定的,“i”的值可以更改,我的return语句将不再工作,因此我必须实现类似的东西
bool myFunction(int i)
{
bool *myArray = //some other array of bools
//should work with any value of i
bool result = myArray[0];
for (int a = 1; a < i; ++a)
{
result &= myArray[i];
}
return result;
}
bool myFunction(int i)
{
bool*myArray=//其他bool数组
//应该与i的任何值一起工作
bool result=myArray[0];
对于(int a=1;a
我想知道除了创建for循环并遍历每个元素并将列表中下一项的值与前两项的存储结果进行比较之外,是否还有更好的方法来实现这一点。像一些容易实现的按位运算符或其他什么东西一样,任何东西都可以去掉循环。您可以使用,它有一个成员函数any()
,如果设置了此位集中的任何位,它将返回true
。如果未设置位,则none()
将返回true
但实施的是:
模板
bool dynamic_位集::any()常量
{
对于(大小_类型i=0;i
所以你有一个for循环
但如果您正在搜索类似并行计算的东西,您可能希望使用:
例如,它有一个可以执行此操作的
template<typename T >
T af::allTrue( const array & in )
//C++ Interface for checking if all values in an array are true.
模板
T af::allTrue(常量数组和in)
//C++接口,用于检查数组中的所有值是否为真。
您可能需要知道&=
不是逻辑运算符;它是“按位and”运算符。只要你只在布尔人身上使用它,我想它就行了;但是C不会阻止1或0以外的值滑入数组,所以您可能不应该这样假设。从语义上讲,如果你在做逻辑运算,或者你想要&&
而不是&
也就是说,您当然可以使用短路来改进您正在做的事情。一旦您找到一个0(false),从那时起,任何事情都不会使您的聚合返回到1(true),因此您最好停止
for (int a = 1; result && a < i; ++a)
{
result &= myArray[i];
}
for(int a=1;result&&a
除此之外,你没有什么可以改进的。不知道为什么你反对循环,但是如果你想组合一个未知数量的值,你必须迭代。您可能会找到(或编写)一些实用函数来执行此操作,但无论如何,它都会在内部使用循环。(除非它试图利用一个向量处理器,该处理器本机执行您想要的操作,否则可能……但这是毫无意义的,如果您确实没有限制值的数量,则仍然会涉及一个循环。)您可以在遇到
false
时立即退出循环:
for (int a = 0; a < i; a++)
{
if (!myArray[i])
return false;
}
return true;
这将在每次迭代中产生一个分支,而不是在每次迭代中产生两个分支
如果允许您分配
i+1
条目,那么您可以摆脱“最后一个条目交换”部分:
您还可以去掉myArray[i]
中嵌入的额外算法:
bool *arrayPtr;
myArray[i] = false;
for (arrayPtr = myArray; *arrayPtr; arrayPtr++);
return arrayPtr != myArray+i;
如果编译器还没有应用它,那么这个函数肯定会这样做。另一方面,这可能会使优化器更难展开循环,因此,如果要检查整个数组而不是首先检查i
元素,则必须验证在生成的汇编代码中……可以使用(将std::begin(myArray)+i
替换为std::end(myArray)
:
#包括
#包括
布尔myFunction(int i)
{
向量myArray={true,true,false,false,true};
返回std::all_of(std::begin(myArray),std::begin(myArray)+i,[](bool elem){return elem;});
}
我会更改for循环的条件,以便在遇到错误值时不会继续,并使用迭代器而不是索引
bool myFunction(int i) {
bool myArray[i] = //some other array of bools
//should workwith any value of i
bool result;
bool * a;
for (a = myArray, result = *a; a < myArray+i && result; result=*(++i)) {}
//no need to use the AND operator since we stop if we meet one false
return result;
}
bool myFunction(int i){
bool myArray[i]=//其他一些bool数组
//应该使用i的任何值
布尔结果;
布尔*a;
对于(a=myArray,result=*a;a
或者,如果您确实喜欢使用索引:
bool myFunction(int i) {
bool myArray[i] = //some other array of bools
//should workwith any value of i
bool result;
unsigned int a;
for (a = 0, result = myArray[a]; a < i && result; result=myArray[++i]) {}
//no need to use the AND operator since we stop if we meet one false
return result;
}
bool myFunction(int i){
bool myArray[i]=//其他一些bool数组
//应该使用i的任何值
布尔结果;
无符号整数a;
对于(a=0,result=myArray[a];a
也许我错了,但是如果不是在位范围操作上,我就不会使用位和赋值(A=),但是它与BoL型并不真正相关。
首先,在标准C++中没有VLAS。我的坏哈,对这个语言还是新的:SHere是一些好的答案:你可以使用STD::向量用于这类事情。但是你想在所有元素上做什么呢?如果这就是你想要的,那么就有一些算法在向量上工作。我只想对数组中的所有项执行“AND”运算,或者执行“or”运算。我想知道是否有比循环数组更有效的方法,我想这取决于如何定义问题。这个问题指定数据结构是一个布尔数组,因此假设可以更改。如果目标是避免循环,那么这所做的就是隐藏它。bool *arrayPtr;
myArray[i] = false;
for (arrayPtr = myArray; *arrayPtr; arrayPtr++);
return arrayPtr != myArray+i;
#include <vector>
#include <algorithm>
bool myFunction(int i)
{
std::vector<bool> myArray = { true, true, false, false, true };
return std::all_of(std::begin(myArray), std::begin(myArray) + i, [](bool elem) { return elem; });
}
bool myFunction(int i) {
bool myArray[i] = //some other array of bools
//should workwith any value of i
bool result;
bool * a;
for (a = myArray, result = *a; a < myArray+i && result; result=*(++i)) {}
//no need to use the AND operator since we stop if we meet one false
return result;
}
bool myFunction(int i) {
bool myArray[i] = //some other array of bools
//should workwith any value of i
bool result;
unsigned int a;
for (a = 0, result = myArray[a]; a < i && result; result=myArray[++i]) {}
//no need to use the AND operator since we stop if we meet one false
return result;
}