Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 函数的all_,用于检查数组部分的所有元素中的条件_C++_Algorithm_Loops - Fatal编程技术网

C++ 函数的all_,用于检查数组部分的所有元素中的条件

C++ 函数的all_,用于检查数组部分的所有元素中的条件,c++,algorithm,loops,C++,Algorithm,Loops,下面的代码检查声明数组中的所有元素是否都是奇数 #include "stdafx.h" #include <iostream> // std::cout #include <algorithm> // std::all_of #include <array> // std::array int main () { std::array<int,8> foo = {3,5,7,11,13,17,19,23};

下面的代码检查声明数组中的所有元素是否都是奇数

#include "stdafx.h"
#include <iostream>     // std::cout
#include <algorithm>    // std::all_of
#include <array>        // std::array

int main () {
    std::array<int,8> foo = {3,5,7,11,13,17,19,23};

    if ( std::all_of(foo.begin(), foo.end(), [](int i){return i%2;}) )
        std::cout << "All the elements are odd numbers.\n";

    return 0;
}
#包括“stdafx.h”
#include//std::cout
#(包括)

我想检查声明数组中从
foo[2]
开始的所有元素是否都是奇数

#include "stdafx.h"
#include <iostream>     // std::cout
#include <algorithm>    // std::all_of
#include <array>        // std::array

int main () {
    std::array<int,8> foo = {3,5,7,11,13,17,19,23};

    if ( std::all_of(foo.begin(), foo.end(), [](int i){return i%2;}) )
        std::cout << "All the elements are odd numbers.\n";

    return 0;
}
foo.begin()
替换为
foo[2]
不起作用。我已经尝试了很多其他的事情来做这项工作,所有非常基本的(非常基本的C++用户在这里),没有成功。我不想为了实现这一点而调整阵列的大小


最终,我要寻找的是一个循环,在该循环中,对数组部分的每个
元素检查一个条件,就像对数组部分的任何元素检查一个条件一样。这在R中是比较容易实现的,我希望它在C++中同样容易实现。

< p>这里不能使用迭代器和元素,它们不代表范围。从更一般的意义上讲,即使尝试使用指向元素的指针和迭代器,也不能很好地定义任何给定容器的所有实现

应该使用
std::next(it.begin(),2)
begin()
迭代器增量到
foo[2]
元素,然后可以使用两个迭代器在该范围内进行迭代

std::all_of(std::next(foo.begin(), 2), foo.end(),
  [](int i){/*...*/})

更为通用,适合迭代器,而不仅仅是随机访问迭代器(例如,用于替代
it.begin()+2
);但对于传递给它的迭代器类型,仍将执行

这里不能使用迭代器和元素,它们不代表范围。从更一般的意义上讲,即使尝试使用指向元素的指针和迭代器,也不能很好地定义任何给定容器的所有实现

应该使用
std::next(it.begin(),2)
begin()
迭代器增量到
foo[2]
元素,然后可以使用两个迭代器在该范围内进行迭代

std::all_of(std::next(foo.begin(), 2), foo.end(),
  [](int i){/*...*/})

更为通用,适合迭代器,而不仅仅是随机访问迭代器(例如,用于替代
it.begin()+2
);但对于传递给它的迭代器类型,仍将执行

foo.begin()
替换为
foo.begin()+2
?谢谢。成功了!我花了一整天在这上面。干杯。
foo.begin()
是一个
迭代器,
foo[2]
是值。将
foo.begin()
替换为
foo.begin()+2
?谢谢。成功了!我花了一整天在这上面。干杯。
foo.begin()
是一个
迭代器
foo[2]
是一个值。与
foo.begin()+2
相比有什么好处吗?对于您拥有的随机访问迭代器来说,它将是一样的。如果容器发生变化,我更喜欢使用next来获得更健壮的代码,以及其他维护变化等,并且在阅读代码时,我个人更喜欢代码的清晰性。这两种方法都能正常工作。@Niall-另一方面,如果容器更改为不提供随机访问迭代器的类型,
std::next
将引入(可能较大的)性能影响,而
begin()+2
将无法编译。如果容器类型或迭代器类型至关重要,带有关于预期类型的明确消息的
static\u assert
begin()+2
上的编译错误要好,该错误可能看起来不相关。与仅
foo.begin()+2
相比有什么好处吗?对于您拥有的随机访问迭代器,它最终将是一样的。如果容器发生变化,我更喜欢使用next来获得更健壮的代码,以及其他维护变化等,并且在阅读代码时,我个人更喜欢代码的清晰性。这两种方法都能正常工作。@Niall-另一方面,如果容器更改为不提供随机访问迭代器的类型,
std::next
将引入(可能较大的)性能影响,而
begin()+2
将无法编译。如果容器类型或迭代器类型至关重要,带有关于预期类型的明确消息的
静态断言
begin()+2
上的编译错误要好,该错误可能看起来不相关。