简体   繁体   English

创建指向更多向量的指针向量

[英]creating a vector of pointers that point to more vectors

I am trying to create a vector that contains pointers, each pointer points to another vector of a type Cell which I have made using a struct. 我正在尝试创建一个包含指针的向量,每个指针指向另一个我使用结构制作的Cell类型的向量。 The for loop below allows me to let the user define how many elements there are in the vector of pointers. 下面的for循环允许我让用户定义指针向量中有多少个元素。 Here's my code: 这是我的代码:

vector< vector<Cell>* >  vEstore(selection);
for (int t=0; t<selection; t++)
{
    vEstore[t] = new vector<Cell>; 
    vEstore[t]->reserve(1000);
}

This, I think, gives me a vector of pointers to destination vectors of the type Cell . 我想,这给了我一个指向Cell类型的目标向量的指针向量。 This compiles but I'm now trying to push_back onto the destination vectors and can't see how to do it. 这编译,但我现在正试图push_back到目标向量,无法看到如何做到这一点。

Since the destination vector is of the type Cell which is made from a type as follows: 由于目标向量是Cell类型,它由以下类型组成:

struct Cell
{
    unsigned long long lr1;
    unsigned int cw2;
};

I can't work out how to push_back onto this destination vector with 2 values? push_back如何使用2个值将push_back到此目标向量?

I was thinking ... 我刚在想 ...

binpocket[1]->lr1.push_back(10);
binpocket[1]->cw2.push_back(12);

As I thought this would dereference the pointer at binpocket[1] revealing the destination array values, then address each element in turn. 我认为这将取消引用binpocket[1]处的指针,显示目标数组值,然后依次寻址每个元素。 But it doesn't compile. 但它没有编译。

can anyone help ...but this only has one value and doesn't compile anyway. 任何人都可以帮助...但这只有一个值,无论如何都不会编译。

Cell cell = { 10, 12 };
binpocket[1]->push_back(cell);

Alternatively, you can give your struct a constructor. 或者,您可以为结构提供构造函数。

struct Cell
{
    Cell() {}
    Cell(unsigned long long lr1, unsigned int cw2)
        : lr1(lr1), cw2(cw2)
    {
    }

    unsigned long long lr1;
    unsigned int cw2;
};

Then you could do 然后你可以做到

binpocket[1]->push_back(Cell(10, 12));

Note that long long is non-standard (yet), but is a generally accepted extension. 请注意, long long是非标准的(但是),但是是一个普遍接受的扩展。

Give your cell a constructor: 给你的单元格一个构造函数:

struct Cell
{
    unsigned long long lr1;
    unsigned int cw2;

    Cell( long long lv, int iv ) : lr1(lv), cw2(iv ) {}
};

You can now say things like: 你现在可以这样说:

binpocket[1]->push_back( Cell( 10, 12 ) );

BTW, note that long long is not standard C++. 顺便说一句,请注意,long long不是标准的C ++。

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

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