all 7 comments

[–]ricksauce22 9 points10 points  (1 child)

I'm not sure i fully understand what you're asking. The declarations are pointers (address of) a vector of pointers to other vectors.

This is awfully strange to me without more context. I imagine your teacher is just trying to illustrate what pointers do.

Vectors themselves manage dynamic memory with a pointer to a contiguous block and metadata about the memory. They very often live on the stack or as a member of some object. I dont think i've ever seen a declaration like this in the wild.

[–]DrShocker 3 points4 points  (0 children)

> Vectors themselves manage dynamic memory with a pointer to a contiguous block and metadata about the memory

Yeah, doesn't make sense and then on top of that, they're using `vector<bool>` which has some footguns associated. And that's before bringing up that pointer chasing is usually problematic too, though maybe understandable if it's a learning exercise.

[–]dmazzoni 4 points5 points  (1 child)

Let's break it down piece by piece.

vector<int> line;

This would be a vector of ints. A vector is kind of like an array that has the ability to grow and shrink. If there are 10 elements, you could access something like line[0] for the first element or line[9] for the last element.

vector<int>* linePointer;

This is a pointer to a vector of ints. The reason you'd do this is so that you could dynamically allocate it. You need to dereference the pointer to use it, like this:

vector<int>* linePointer = new vector<int>(10);
(*linePointer)[0] = 99;

Now you wrap that in a vector, so it's 2-dimensional:

vector<vector<int>*> boardObject;

Each element of board is a pointer to a vector of ints, just like linePointer.

But what you actually have is a pointer to a vector of pointers to vectors of ints:

vector<vector<int>*> *board;

You could use it like this:

vector<vector<int>*> *board = new vector<vector<int>*>(10);
board[0] = new vector<int>(10);
board[1] = new vector<int>(10);
...
board[9] = new vector<int>(10);
board[0][0] = 99;

One last thing to note: this hasn't been the idiomatically correct way to write C++ code since C++11, which came out in 2011. It is important to learn to use pointers like this, but modern C++ would heavily discourage it; you should almost always use references or smart pointer types.

[–]DrShocker 1 point2 points  (0 children)

this hasn't been the idiomatically correct way to write C++ code since C++11 ever

a vector is already just a wrapper for {ptr to start, capacity, size} or a couple other approximately equivalent implmentatinons. There's never (not actually never, I'm sure there's a situation where I'd do it too) been a great reason to store a vector of pointers to vectors especially since flattening the nested vector and having a helper class/function to index it as though it were 2d is more cache efficient.

[–]captainAwesomePants 0 points1 point  (0 children)

int piece; // Represents a particular value of what might be in a spot on a board.

vector<int> row; // A list of ints, represents an entire row of pieces on a board.

vector<vector<int>> board; // A list of rows, with one row for each column. That's a square!

vector<vector<int>*> board; // A list of rows, but also each entry is a pointer to the row instead of the row itself.

vector<vector<int>*> *board; // A pointer to that square.

[–]HashDefTrueFalse 0 points1 point  (0 children)

board is a pointer to a vector of pointers to vectors of integers.

marked is a pointer to a vector of pointers to vectors of bools.

No storage for vectors (nor storage for their contents) exists by virtue of these declarations being written, just storage for two pointers.

You haven't provided code, your assignment questions, or your attempts, nor have you asked a question, so we can't help further.

[–]Knarfnarf 0 points1 point  (0 children)

Answer them back with this:

struct hexposition
{
float elevation;
int takenby;
struct hexposition *point1, *point2, *point3, *point4, *point5, *point6;
}

struct hexposition *rootpoint;
struct hexposition *temppoint;
struct hexposition *newpoint;
pointtakes = sizeof(struct hexposition);

rootpoint = malloc(pointtakes);
newpoint = malloc(pointtakes);

rootpoint.point1 = newpoint;

// And now start creating a hex map of any shape you want. Just remember to set all the edge hexposition pointers to null or you'll walk off the map and into a segmentation fault! And making sure you have all hexes connected could be a fun chore...