UnworthyToast
UnworthyToast

Reputation: 825

Why does my vector print out all zeroes?

I have this preliminary code:

#include "std_lib_facilities_4.h"

void numbers()
{
    vector<int> first(9);
    for (int i = 0; i <= 9; ++i)
    {
            cout << i << endl;
            first.push_back(i);
    }
    for (int j = 0; j <= 9; ++j)
            cout << first[j];
}

int main()
{
        numbers();
}

I was hoping to get the number 1, 2, 3, 4, 5, 6, 7, 8, and 9 to print out when I printed each element. Instead I get this:

0
1
2
3
4
5
6
7
8
9
0000000000

What am I doing wrong?

Upvotes: 0

Views: 2035

Answers (4)

herohuyongtao
herohuyongtao

Reputation: 50667

Did you realize that first will be size of 19? And the first 9 values will all be 0.

The reason is that vector<int> first(9); initializes it with size 9 with all values 0. Then in the for loop, each time you do push_back(), you are adding a new value (make its size +1) to it.

Two solutions:

  1. Change

    vector<int> first(9);
    

    to

    vector<int> first;
    
  2. Alternatively, change

    vector<int> first(9);
    ...
    first.push_back(i);
    

    to

    vector<int> first(10);
    ...
    first[i] = i;
    

Upvotes: 2

Vlad from Moscow
Vlad from Moscow

Reputation: 311058

At first you created a vector with 9 elements which was initialized by 0 if you did not explicitly specify an initializer yourself

vector<int> first(9);

So now vector first contains 9 elements each of which is initialized by 0.

Then you add new 10 elements at the end of the vector using method push_back

for (int i = 0; i <= 9; ++i)
{
        cout << i << endl;
        first.push_back(i);
}

So now the vector contains 9 initial zeroes and then 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. That is its first 10 elements are equal to 0.

And in this loop

for (int j = 0; j <= 9; ++j)
        cout << first[j];

you output the first 10 elements that is ten zeroes.

If you want to have only 10 elements in the vector then you could write either

void numbers()
{
    vector<int> first;
    first.reserve( 10 );

    for (int i = 0; i <= 9; ++i)
    {
            cout << i << endl;
            first.push_back(i);
    }
    for (int j = 0; j <= 9; ++j)
            cout << first[j];
}

or

void numbers()
{
    vector<int> first( 10 );

    for (int i = 0; i <= 9; ++i)
    {
            cout << i << endl;
            first[i] = i;
    }
    for (int j = 0; j <= 9; ++j)
            cout << first[j];
}

Upvotes: 0

6502
6502

Reputation: 114559

When you call push_back a new element is added to the vector, so you start with 9 zeros in it and then add the numbers from 0 to 9 AFTER the zeros.

push_back works by adding the specified element and increasing the vector size by one.

The number you pass with creating a vector is the initial size and, for a vector of int, all the values will be initially 0.

Upvotes: 0

user229044
user229044

Reputation: 239382

You're initializing the vector with 9 empty values (0) and then pushing back 10 new values (the first of which is 0), yielding a vector of 19 elements, the first 10 of which are all still 0. This is what first(9) does, creates a vector of 9 elements.

Instead of push_back, just set the element to the new value:

for (int i = 0; i <= 9; ++i)
    first[i] = i;

Upvotes: 5

Related Questions