What do these vector pointer declarations mean?

0
8
Asked By CuriousCoder42 On

I'm working on an assignment that includes these two declarations: `vector<vector* > *board;` and `vector<vector* > *marked;`. I'm a bit confused about what exactly they are doing. I understand they are pointers, but I'm lost on the specifics. Could someone explain what these structures are and how they work?

3 Answers

Answered By CodeNinja88 On

To break it down, `vector<vector* > *board;` is a pointer to a vector that holds pointers to other vectors of integers. Essentially, this means that each element of `board` is a pointer to a different vector of ints. The idea behind using pointers here is likely to conserve memory allocation or allow dynamic resizing. In practice, it can get complicated, especially with pointers pointing to other pointers.

Answered By ProgrammerKate On

In the two declarations you provided, `marked` works similarly to `board`. It's a pointer to a vector of pointers, but for boolean values instead. In both cases, the pointers imply that no actual vector storage exists until you allocate memory for them. Just declaring them doesn't create the vectors; you need to initialize them to use them effectively. It might seem complex, but understanding pointers in this context is essential for deeper learning about C++.

Answered By TechieTommy On

When you see `vector<vector* >`, think of it as a 2D structure where each entry is actually pointing to another vector of integers. You might use it for situations where you need dynamic row sizes. Just remember, if you're using pointers like this, you need to carefully manage memory to avoid leaks. Also, this style isn't the most common nowadays, given the improvements in C++ with smart pointers and better memory management techniques.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.