Loops 如何使用“输入”;“基于范围的循环”;

Loops 如何使用“输入”;“基于范围的循环”;,loops,for-loop,c++11,cin,range-based-loop,Loops,For Loop,C++11,Cin,Range Based Loop,包含“cin”的循环不会更改值。循环内部发生了什么?我如何使用这个(基于范围的循环)来获取输入 输入:- #include<iostream> using namespace std; int main() { int arr[]={1,2,3,4,5}; //with outputs/cout it work properly for(auto x:arr) { cout<<x<<" "

包含“cin”的循环不会更改值。循环内部发生了什么?我如何使用这个(基于范围的循环)来获取输入

输入:-

#include<iostream>
using namespace std;

int main()
{
    int arr[]={1,2,3,4,5};

    //with outputs/cout it work properly
    for(auto x:arr)
    {
        cout<<x<<" ";
    }
    cout<<endl;


    //Now suppose, I want to change them
    for(auto x:arr)
    {
        cin>>x;
    }

    //now again printing
    for(auto x:arr)
    {
        cout<<x<<" ";
    }
}
int main()
{
    int arr[4];

    //INPUT VALUES
    for(auto &x:arr)
    {
        cin>>x;
    }

    //PRINTING VALUES
    for(auto x:arr)
    {
        cout<<x<<" ";
    }
}

很简单!!我们只需要使用ampersand操作符来创建变量

输入:-

#include<iostream>
using namespace std;

int main()
{
    int arr[]={1,2,3,4,5};

    //with outputs/cout it work properly
    for(auto x:arr)
    {
        cout<<x<<" ";
    }
    cout<<endl;


    //Now suppose, I want to change them
    for(auto x:arr)
    {
        cin>>x;
    }

    //now again printing
    for(auto x:arr)
    {
        cout<<x<<" ";
    }
}
int main()
{
    int arr[4];

    //INPUT VALUES
    for(auto &x:arr)
    {
        cin>>x;
    }

    //PRINTING VALUES
    for(auto x:arr)
    {
        cout<<x<<" ";
    }
}