0

I am just wondering what does this do

for (size_t pos : vec)

I understand that it is a for loop, but how does it work? How would you interpret it? Is there a long version of it, like:

for(size t pos = 0 ; 0 < vec...)
J J
  • 53
  • 7
  • You're looking for the [range-based for loop](https://en.cppreference.com/w/cpp/language/range-for). (Hard to search for if you don't know what it is called.) – Eljay Mar 22 '20 at 17:35
  • [In for (int val :arr), what does the colon mean?](https://stackoverflow.com/q/47275141/995714), ['colon' and 'auto' in for loop c++? need some help understanding the syntax](https://stackoverflow.com/q/35490236/995714), [What is “for (x : y)”?](https://stackoverflow.com/q/24946027/995714) – phuclv Mar 22 '20 at 17:40

1 Answers1

2

It is a range-based loop. "pos" takes values from vec begin to end. For example the following code prints 1,10,100 and 1000.

#include <iostream>

using namespace std;

int main()
{
    int number[] = {1,10,100,1000};

    for (int pos : number)
    {
        cout << pos << endl;
    }

    return 0;
}