C++ range-for-statement

#C++

In C++ we can use range-for-statement

# include <iostream>
using namespace std;
int main(){
    int v[] = {1,3,5,7};

    for( auto x:v)
    {
      cout << x << endl;
   }
}

What this does is to copy each element of array v into x and print it. For efficiency, we could use instead pointers.

for(auto& x : v) { cout << &x << endl; }

Published: by ;

L Ma (2017). 'C++ range-for-statement', Datumorphism, 09 April. Available at: https://datumorphism.leima.is/til/programming/cpp/cpp-range-for-statement/.

Current Ref:

  • til/programming/cpp/cpp-range-for-statement.md