Can anybody tell me why do we need a range based for loop?

Can anybody tell me why would anybody need to use a range based for loop?

why would anybody want to run the loop for over all elements in the given range?
Well, you already illustrated such a case previously, when printing all the elements in a vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int>v;
    v.push_back(10);
    v.push_back(21);
    v.push_back(32);
    v.push_back(43);
    v.push_back(90);

    for (size_t i=0;i<v.size(); i++)
    {
        cout<<v[i]<<endl;
    }

    cout<<endl;
    
    for (int a : v)
        cout << a << ' '; 

    return 0;
}
ohhh I get it =D
Topic archived. No new replies allowed.