Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.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
for(auto&;it:a)是什么意思? 我是新手,学习如何用更灵活的方式使用C++语言。_C++_For Loop_Vector_Operators - Fatal编程技术网

for(auto&;it:a)是什么意思? 我是新手,学习如何用更灵活的方式使用C++语言。

for(auto&;it:a)是什么意思? 我是新手,学习如何用更灵活的方式使用C++语言。,c++,for-loop,vector,operators,C++,For Loop,Vector,Operators,在一个竞赛题中,我看到有人写了这样的代码: #include <bits/stdc++.h> using namespace std; int main() { #ifdef _DEBUG freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif int t; cin

在一个竞赛题中,我看到有人写了这样的代码:

#include <bits/stdc++.h>
using namespace std;

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
//  freopen("output.txt", "w", stdout);
#endif
    
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        vector<int> a(n);
        for (auto &it : a) cin >> it;
        sort(a.begin(), a.end());
        bool ok = true;
        for (int i = 1; i < n; ++i) {
            ok &= (a[i] - a[i - 1] <= 1);
        }
        if (ok) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}
#包括
使用名称空间std;
int main(){
#ifdef_调试
freopen(“input.txt”,“r”,stdin);
//freopen(“output.txt”,“w”,stdout);
#恩迪夫
int t;
cin>>t;
而(t--){
int n;
cin>>n;
向量a(n);
对于(自动&it:a)cin>>it;
排序(a.begin(),a.end());
bool ok=true;
对于(int i=1;i它可以工作

  • 我也不理解表达式
    ok&=(a[I]-a[I-1]的语法:

    for (auto& it : a)
    
    称为基于范围的循环

    它用于操纵
    a
    数组或向量中给定的每个元素,元素的类型将与
    a
    的类型等效

    另一方面,
    &
    规定,对
    a
    中的任何元素
    进行修改的任何内容最初都将被替换,并且不会制作任何副本供临时使用(即通过引用)

    简而言之,如果元素的值为
    3
    ,并且将其更改为
    5
    ,则原始数组或向量将受到影响


    ok&=(a[i]-a[i-1]你有一个
    向量x
    ,然后你想在每个元素上迭代

    您可以使用旧的“c”样式执行以下操作:

    for (int i=0; i<x.size(); ++i)
    
    其结果是
    i
    是从向量中的元素获取的副本 因此,如果您需要参考,请执行以下操作:

    for (T& i:x)
    
    这里是代码的答案,因为c++11可以自动推断i的类型 对语法进行遍历

    for (auto& i : x)
    
    1.

    (auto &it : a) cin >> it 
    
    (a[i] - a[i - 1] <= 1)
    
    表示您输入向量
    a

    它类似于:

    for(int i = 0 ; i < n; i++){
        cin>>a[i]; 
    }
    
    结果布尔值1(如果条件为真)或0(如果条件为假)


    ok&=(a[i]-a[i-1]这是一个基于范围的for循环:另外,阅读如何在线搜索可以更好地回答这类问题:拼出标点名称->可能重复的标点名称对于
    auto
    ,不必提到C++11,基于范围的循环在C++11之前也不可用。@mch我提到auto,因为它只在C++11之后才能工作,but谢谢:)!明白了。&符号把我弄糊涂了,否则我想我理解它了&是按位逻辑的and运算,比如12&5.12->1100和5=>0101,所以a&b=0100即4AA,你能告诉我为什么我在使用不同的方法查找最小元素时会得到不同的结果吗?这两种方法都能正确地查找最小元素。你能告诉我这个例子吗结果是不同的,我现在得到了那个部分,但是ok呢&?@Maisha
    ok?=(exp)
    相当于按位
    ok?(exp)
    (auto &it : a) cin >> it 
    
    for(int i = 0 ; i < n; i++){
        cin>>a[i]; 
    }
    
    (a[i] - a[i - 1] <= 1)
    
    ok &= (a[i] - a[i - 1] <= 1)