C++ range-for-statement


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 ;

Table of Contents

Current Ref:

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