简体   繁体   English

使用push_back的C ++向量

[英]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. 我注意到,如果我使用push_back在向量中插入整数,则得到的答案是1,但是如果我通过vector [0]将整数插入到向量中,则会得到正确的答案。 My question is, is push_back doing more then just inserting my integer into the vector? 我的问题是,push_back会做更多的事情,然后将我的整数插入向量中吗?

#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.---- ---- 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);  

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. vector<int>::push_back方法将始终添加到向量中,因此,当您使用push_back方法时,实际上是将向量的大小增加了一个。

The constructor accepting a size_t object is setting the initial size of the vector, but populating it with default constructed objects. 接受size_t对象的构造函数正在设置向量的初始大小,但使用默认构造的对象填充该向量。

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. 您只需要对向量使用默认构造函数即可。 eg 例如

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. 而且,如果您需要优化,则可以使用reserve方法来预分配内存,这是我假设您正在尝试做的事情。

In the 2nd one you are creating vectors of a certain size. 在第二个中,您将创建一定大小的向量。 You are then using push_back. 然后,您正在使用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. 向量键从大小3开始,然后当您进行3次push_back调用时,向量将包含6个元素。

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. 将数据插入向量。

The push_back() insert elements into the container. push_back()将元素插入容器。 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, eg: 为了获得与在构造函数中设置大小然后设置值相同的结果,您需要将初始向量构造为空,例如:

std::vector<int> redDoors;

Push_back inserts an element in the end of the vector. Push_back在向量的末尾插入一个元素。 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;

The push_back method appends the given element value to the end of the container. push_back方法将给定的元素值附加到容器的末尾。

The constructor of the type explicit vector( size_type count ); 类型explicit vector( size_type count );constructor explicit vector( size_type count ); constructs the container with count value-initialized (default constructed, for classes) instances of T. No copies are made. 使用T的计数值初始化 (对于类为默认构造)实例构造容器 。不进行任何复制。

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 : 如果您希望获得与使用operator[]访问向量时相同的行为,请执行以下operator[]

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 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM