Skip to content

Instantly share code, notes, and snippets.

@smamran
Last active January 26, 2016 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smamran/9498d0f719f3fe07ff8d to your computer and use it in GitHub Desktop.
Save smamran/9498d0f719f3fe07ff8d to your computer and use it in GitHub Desktop.
Range Based For Loop C++

Range Based For Loop C++

#include <iostream>
#include <vector>
 
int main() 
{
    std::vector<int> v = {0, 1, 2, 3, 4, 5};
 
    for(const int &i : v) // access by const reference
        std::cout << i << ' ';
    std::cout << '\n';
 
    for(auto i: v) // access by value, the type of i is int
        std::cout << i << ' ';
    std::cout << '\n';
 
    for(auto&& i: v) // access by reference, the type of i is int&
        std::cout << i << ' ';
    std::cout << '\n';
 
    for(int n: {0, 1, 2, 3, 4, 5}) // the initializer may be a braced-init-list
        std::cout << n << ' ';
    std::cout << '\n';
 
    int a[] = {0, 1, 2, 3, 4, 5};
    for(int n: a)  // the initializer may be an array
        std::cout << n << ' ';
    std::cout << '\n';
 
    for(int n: a)  
        std::cout << 1 << ' '; // the loop variable need not be used
    std::cout << '\n';
 
}

Output

0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
0 1 2 3 4 5
1 1 1 1 1 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment