vendredi 23 octobre 2020

Ambiguity in Vector Size

I've just started using C++ stl's vector containers and I noticed this ambiguity in the vector size.

#include<iostream>
#include<vector>

int main(){
    int n=5;
    std::vector<int> vec(n);
    for(int i=0;i<n;i++){
        vec.push_back(i*3);
    }


    
    std::cout<<vec.size()<<endl;
    for(auto it:vec){
        std::cout<<it<<" ";
    }
    return 0;
}

OUTPUT

10
0 0 0 0 0 0 3 6 9 12

As far as I know the vector size should have been 5 but it seems there's something wrong with either the compiler or is it that I'm missing something essential in vectors ?

Aucun commentaire:

Enregistrer un commentaire