why vector resize its size when i take input same element in the vecotor

//here if i take input n=5 then inserting in vec1 1,2,3,4, 5 then it consider in output only 1,2,3,4 according to its size but when i tried to insert in vec1 1,2,2,2,3,4 then its output is 1,2,2,2,3,4 how? as its size is 4.

#include<bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin>>n;
    vector<int> vec1;
    for (int i=0; i<n; ++i){
        cin>>i;
        vec1.push_back(i);
    }
    sort(vec1.begin(), vec1.end());
    vector<int> :: iterator it;
    for( it = vec1.begin(); it !=vec1.end(); it++)
        cout<<*it<<" ";
        cout<<vec1.size();

}

Upvotes: 0

Views: 33

Answers (1)

1201ProgramAlarm
1201ProgramAlarm

Reputation: 32732

cin>>i will overwrite your loop variable (so the loop terminates after you input a number that is >= n - 1).

You need to use a different variable to get your input.

int x;
cin >> x;
vec1.push_back(x);

Upvotes: 1

Related Questions