user2510809
user2510809

Reputation: 245

c++ vector using push_back

I noticed that if I use push_back to insert ints in the vector, I get an answer off by 1 but If I insert integers into my vector by vector[0] i get the right answer. My question is, is push_back doing more then just inserting my integer into the vector?

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

typedef vector<int> vi;

class KeyDungeonDiv2 {
public:

    int countDoors(vi doorR, vi doorG, vi keys)
    {
        int r = keys[0];
        int g = keys[1];
        int w = keys[2];
        int numdoors = 0;

        for(int i = 0; i < doorR.size(); i++) {
            if(r >= doorR[i] && g >= doorG[i])
                numdoors++;
            else if (r < doorR[i] && g >= doorG[i]) {
                if(r + w >= doorR[i]) {
                    numdoors++;
                }
            }
            else if(r >= doorR[i] && g < doorG[i]) {
                if(g + w >= doorG[i]) {
                    numdoors++;
                }
            }
            else if (r < doorR[i] && g < doorG[i]) {
                if(w >= (doorR[i] - r ) + (doorG[i] - g)) {
                    numdoors++;
                }
            }
            else if(doorR[i] == 0 && doorG[i] == 0)
                    numdoors++;
        }
        return numdoors;
    }
};

int main()
{
    vector<int> redDoors (4);
    redDoors[0] = 2;
    redDoors[1] = 0;
    redDoors[2] = 5;
    redDoors[3] = 3;

    vector<int> greenDoors(4);
    greenDoors[0] = 1;
    greenDoors[1] = 4;  
    greenDoors[2] = 0;  
    greenDoors[3] = 2;  

    vector<int> keys (3);
    keys[0] = 2;    
    keys[1] = 3;    
    keys[2] = 1;    

    KeyDungeonDiv2 d;
    cout << d.countDoors(redDoors,greenDoors,keys) << endl;

    return 0;
}

----vs.----

vector<int> redDoors (4);
redDoors.push_back(2);
redDoors.push_back(0);
redDoors.push_back(5);
redDoors.push_back(3);

vector<int> greenDoors(4);
greenDoors.push_back(1);
greenDoors.push_back(4);    
greenDoors.push_back(0);    
greenDoors.push_back(2);    

vector<int> keys (3);
keys.push_back;
keys.push_back(3);  
keys.push_back(1);  

Upvotes: 1

Views: 2579

Answers (5)

gokhans
gokhans

Reputation: 210

Push_back inserts an element in the end of the vector. But if the vector is full, it allocates new memory with increased capacity and copies the vector to new memory. But you are creating your vectors with initial sizes. You need to create them without these initial sizes as below

std::vector<int> foo;

Upvotes: 1

Pierre Fourgeaud
Pierre Fourgeaud

Reputation: 14510

The push_back method appends the given element value to the end of the container.

The constructor of the type explicit vector( size_type count ); constructs the container with count value-initialized (default constructed, for classes) instances of T. No copies are made.

So when you do :

vector<int> redDoors (4); // Create a vector of 4 ints initialized at 0
redDoors.push_back(2);    // Adds 2 at the end
redDoors.push_back(0);    // Adds 0 at the end
redDoors.push_back(5);    // Adds 5 at the end
redDoors.push_back(3);    // Adds 3 at the end

// Result :
// [ 0, 0, 0, 0, 2, 0, 5, 3 ]

If you want the same behaviour as when you access your vector with the operator[], just do :

vector<int> redDoors;     // Just create a vector
redDoors.push_back(2);    // Adds 2 at the end
redDoors.push_back(0);    // Adds 0 at the end
redDoors.push_back(5);    // Adds 5 at the end
redDoors.push_back(3);    // Adds 3 at the end

// Result :
// [ 2, 0, 5, 3 ]

Upvotes: 1

Karl Nicoll
Karl Nicoll

Reputation: 16419

The vector<int>::push_back method will always add to the vector, so when you use the push_back method, you're essentially increasing the size of the vector by one.

The constructor accepting a size_t object is setting the initial size of the vector, but populating it with default constructed objects.

So what you're doing is this:

vector<int> redDoors(4);
// redDoors = [ 0 | 0 | 0 | 0 ]

redDoors.push_back(2);
// redDoors = [ 0 | 0 | 0 | 0 | 2 ]


redDoors.push_back(0);
// redDoors = [ 0 | 0 | 0 | 0 | 2 | 0 ]

// ... and so on...

What you need to do is simply use the default constructor for your vectors. e.g.

vector<int> redDoors;

And if you need the optimization, you can use the reserve method to pre-allocate memory, which is what I assume you were trying to do.

Upvotes: 4

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153792

The push_back() insert elements into the container. To get the same result as setting the size in the constructor and then setting the value you need to construct the initial vectors as empty, e.g.:

std::vector<int> redDoors;

Upvotes: 3

bruvel
bruvel

Reputation: 396

In the 2nd one you are creating vectors of a certain size. You are then using push_back. This is inserting the value to the end of the vector. The vector keys starts out with size 3, then when you do the 3 push_back calls the vector will have 6 elements.

Don't initialize the vectors to have a starting size. For the vector keys do this:

vector<int> keys;

You can then use

keys.push_back(5);

to insert data into the vector.

Upvotes: 3

Related Questions