vendredi 9 juin 2017

Two separate "for" loops for vector inside same scope, prints just the first loop

This is the demonstrating code:

#include<iostream>
#include<vector>
using namespace std;

int main() {
    vector<int> a;
    a.push_back(1);
    a.push_back(2); 
    a.push_back(3);
    a.push_back(4);
    a.push_back(5);

    int size = a.size();
    cout << "The size of the vector: " << size << "\n";

    for (auto i = 0; i != 6; i++) {
        cout << a[i] << endl;
    }

    //Based on range based for loop
    for (auto j : a) {
        cout << j ;
    }

    return 0;
}

After the run, the first loop prints vector values. Then the second loop skips (Without any condition).

This is pretty strange [May be not ?].

Can someone please explain this behavior.

Aucun commentaire:

Enregistrer un commentaire