C++ 对于c+中的每个计数器循环+;不';t以一定的数字递增

C++ 对于c+中的每个计数器循环+;不';t以一定的数字递增,c++,arrays,for-loop,foreach,counter,C++,Arrays,For Loop,Foreach,Counter,我正在开发一个程序,它使用for-each循环对数组进行排序。我在为每一个循环胡乱处理以刷新我对它的记忆,我遇到了一个有趣的问题。这是我的代码 #include <iostream> #include <conio.h> using namespace std; int main() { int arr [4] = {5, 9, 23, 45}; int const quote = 10; int failed = 0; for eac

我正在开发一个程序,它使用for-each循环对数组进行排序。我在为每一个循环胡乱处理以刷新我对它的记忆,我遇到了一个有趣的问题。这是我的代码

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{

    int arr [4] = {5, 9, 23, 45};
    int const quote = 10;
    int failed = 0;
    for each (int i in arr)
    {
        int x = arr[i];
        if (x < quote)
        {
            failed += 1;
        }
        x = 0;
    }
    cout << failed;
    _getch();

    return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
int-arr[4]={5,9,23,45};
int const quote=10;
int失败=0;
对于每个(arr中的int i)
{
int x=arr[i];
如果(xcout此页面可能对您有所帮助。您可以对大多数排序算法使用内置函数


< /P>这不象C++,大概是C++ +CLI。我很确定,当你为每个(<我)在ARR中< <代码> <代码>时,<代码> i <代码>取数组元素的值,而不是它们的索引。也就是说,<代码> i <代码>第一次迭代时为5,第二次为9。

然后索引出界并读取一些随机垃圾。谢谢,我刚刚意识到我正在为我的项目使用固定数量的值,所以我将使用常规for循环。