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. The for loop below allows me to let the user define how many elements there are in the vector of pointers. 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
. This compiles but I'm now trying to push_back
onto the destination vectors and can't see how to do it.
Since the destination vector is of the type Cell which is made from a type as follows:
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?
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. 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.
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++.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.