-4
int main()
 {

    long int n;
    long int k;
    long int q;
    long int o;
    long int temp;

    cin >> n >> k >> q;
    vector<int> a(n);
    vector<int> b(n);
    for( int a_i = 0;a_i < n;a_i++){
       cin >> a[a_i];
    }   

    for(long int j=k;j>0;j--) {
        b.push_back  (a[n-j]);        
    }

    for(long int r = 0;r<n-k;r++)
        b.push_back(4);    

    for(long int a0 = 0; a0 < q; a0++){
        long int m;
        cin >> m;
        cout<<b[m]<<endl;
    }
    return 0;
}

Question: In this code the push_back is only inserting 0s in the vector "b" no mater what value I try to put in . why is this so?

S___V
  • 5
  • 2

1 Answers1

1

Your question is not complete in the sense that I'm not sure what values you're passing to the code and what output you're getting. However, I should mention that when you construct b with vector<int> b(n) it will construct a vector of size n with all 0s. Then push_back will add elements to it, but if you print any of the first n elements they will all be 0.

You need to replace

vector<int> a(n);
vector<int> b(n);

with

vector<int> a;
a.reserve(n);
vector<int> b;
b.reserve(n);
Danh
  • 5,916
  • 7
  • 30
  • 45
grigor
  • 1,584
  • 1
  • 13
  • 25