-5
#include <iostream>
#include <vector>
#include <bits/stdc++.h> 

using std::vector;
using std::cin;
using std::cout;

int main(){
    vector <long long> vec(2000,1);
    vec.reserve(2000);
    int count = 0;
    vec.push_back(31);
    for (int i = 0; i < vec.size(); ++i){
        count ++;
    }
    cout << count << " " << vec.max_size() << " " << vec.at(1543) << " " << vec.at(1000);

    return 0;
}

In this code I can't get 1000th element which is 31. I get 1 instead of it. I can use push_back() for elements which index is less than 1000.

Thanks in advance

1 Answers1

3

The 1000th element is actually just 1 due to this constructor.

vector <long long> vec(2000,1);  // fill with 2000 elements, each set to 1

Then after this line

vec.push_back(31);  // element [2000] is now 31

Therefore your final vector is

{1, 1, 1, ..., 1, 1, 31}     // length 2001 elements
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218