When I try to display the values of a vector some more zeroes appear

Asked

Viewed 41 times

1

I want to display some values of my vector, but before displaying the values are appearing some zeros.

#include <iostream>
#include <vector>

using namespace std;

int main(){

  int size_vector, new_value;

  cin >> size_vector;
  vector<int>values(size_vector);

  for(int i = 0; i < size_vector; i++){
    cin >> new_value;
    values.push_back(i);
  }

  for(unsigned int i = 0; i < values.size(); i++){
    cout << values.at(i) << endl;
  }

  return 0;
}

2 answers

2

If you are going to add the elements in the array it makes no sense to initialize it with a size, doing so is creating elements with value without definition, these zeros that are printing (but could not even be zeros, this is memory junk) and then printing out what’s being added. In addition it is adding the value of the loop counter (i) and not the value entered (new_value). I took advantage and changed one of the ties and did it in a more idiomatic and performative way to C++.

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

int main(){
    int size_vector, new_value;
    cin >> size_vector;
    vector<int> values;
    for (int i = 0; i < size_vector; i++) {
      cin >> new_value;
      values.push_back(new_value);
    }
    for (auto item : values) cout << item << endl;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

On the other hand it may be that you want to initialize the vector and not add elements, then it would be another code and maybe not even use a vector, but I will not change this because I do not know the requirements. Join Initializing and filling the data in the elements already created. I would go that way because it’s simpler and faster. There is a small possibility to change the algorithm and create some problem if not careful since the elements are being created and must always be filled to avoid littering.

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

int main() {
    int size_vector;
    cin >> size_vector;
    vector<int> values(size_vector);
    for (int i = 0; i < size_vector; i++) {
        cin >> values[i];
    }
    for (auto item : values) cout << item << endl;
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • I couldn’t even do it cin >> values[i] there and remove the new_value?

  • Yes, I would in the second, I can do

0


Let’s assume I informed you that I desire 5 elements, so size_vector will be 5.

On the line

vector<int>values(size_vector)

You define an integer vector of size 5 initialized with 0. That is, it would have something like:

values = [0, 0, 0, 0, 0]

Inside your loop you use the method push_back to change its vector. See documentation:

Add element at the end

Adds a new element at the end of the vector, after its Current last element. The content of val is copied (or Moved) to the new element.

This effectively increases the container size by one, which causes an Automatic reallocation of the allocated Storage space if -and only if- the new vector size surpasses the Current vector Capacity.

That is, the value will be added to the end of the vector by reallocating the memory needed to store it. If I inform the values from 1 to 5 in the input, its vector would be:

values = [0, 0, 0, 0, 0, 1, 2, 3, 4, 5]

That doesn’t seem to be what you want.

If you just want to change the values in the array you can do:

for(int i = 0; i < size_vector; i++){
    cin >> new_value;
    values.at(i) = new_value;
}

Since the method at can return a reference. Once vector implements the operator [] which can also return a reference, make values[i] = new_value it is also possible.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.