-1

I'm a newbie and learning how to use the c++ language in a more flexible way.

In a contest problem I have seen someone write code like this:

#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;
}

I'm assuming that this line vector<int> a(n); declares a vector and allocates space.

My doubts are:

  1. I'm confused on how this for (auto &it : a) cin >> it; works.

  2. I also don't understand the expression ok &= (a[i] - a[i - 1] <= 1);.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
Maisha
  • 45
  • 1
  • 1
  • 8
  • 2
    It's a range-based for loop: https://en.cppreference.com/w/cpp/language/range-for – UnholySheep Aug 06 '20 at 07:48
  • 3
    Also this type of question is better answered by reading [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – UnholySheep Aug 06 '20 at 07:49
  • 3
    Possible duplicate of ['colon' and 'auto' in for loop c++? need some help understanding the syntax](https://stackoverflow.com/questions/35490236/colon-and-auto-in-for-loop-c-need-some-help-understanding-the-syntax) – user202729 Aug 06 '20 at 07:51

3 Answers3

4

1.

for (auto &it : a) cin >> it 

means you are taking the input of vector a.

It's similar to:

for(int i = 0 ; i < n; i++){
    cin>>a[i]; 
}

The auto keyword specifies that the type of the variable that is being declared will be automatically deducted from its initializer. So here it takes the references of a[i] and works instead of a[i] as it.

2.

(a[i] - a[i - 1] <= 1)

results boolean 1 (if condition is true) or 0 (if condition is false).

ok &= (a[i] - a[i - 1] <= 1) 

means ok = ok & 1 or ok = ok & 0, depending on the condition true or false.

Jayanth
  • 115
  • 1
  • 11
Hridoy_089
  • 318
  • 3
  • 11
  • got it! the & sign confused me, otherwise i think i understood it – Maisha Aug 06 '20 at 08:09
  • & is bitwise logical and operation like 12 & 5 . 12->1100 and 5=>0101 so a&b = 0100 i.e 4 – Hridoy_089 Aug 06 '20 at 08:32
  • aaand can you tell me why i get different result when i use different method of finding the min element ? https://pastebin.com/DBd594vb – Maisha Aug 06 '20 at 08:59
  • both method is correct to find minimum element. can you let me know the case for what the result is different – Hridoy_089 Aug 06 '20 at 09:29
  • yes https://codeforces.com/contest/1399/problem/B – Maisha Aug 06 '20 at 14:26
  • ok the problem is when you used sort() function the order of the vector had changed. In the given problem order should not be changed. But when you use *min_element() function it will give you min element without sorting. Hope you clear :) – Hridoy_089 Aug 07 '20 at 10:47
2

The syntax:

for (auto& it : a)

is called a range-based loop.

It's used to manipulate each element given in a array or vector and the type of the element will be equivalent to the type of the a.

OTOH, the & states that anything modified to any of the element it in a will be originally replaced and no copy will be made for temporary use (i.e. by reference).

In short words, if an element has value 3 and if you change it to 5, the original array or vector will get affected.


ok &= (a[i] - a[i - 1] <= 1);

is a equivalent to isn't a reference to operator, it's bitwise in this context:

ok = ok & (a[i] - a[i - 1] <= 1);

Since the type of ok is a bool, only true (1) or false (0) could be held.

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
2

you have a vector<T> x then you want to iteate over every element

you could do in the old "c" style something like:

for (int i=0; i<x.size(); ++i)

but you could also use a range-based loop doing:

for (T i:x)

with the consequence that i is a copy taken from the element in the vector so if you need instead the reference you do:

for (T& i:x)

AND here the answer to your code, since c++11 you can infer the type of i auto-matically ergo the syntax

for (auto& i : x)
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97