3

I'm trying to initiate a 3d vector in c++. I'm wanting it to initially be a 100 by 100 by 1, but I'm running into problems. (I know that's 2d basically, but I'll resize the z axis vector later.)

I have a class, Entity, that the vector should contain a pointer to. I've included the Entity vector headers correctly, but it's still not working. Here's what I'm trying to do:

vector <vector <vector <Entity* > > > matrix (100, vector < vector < Entity* > > (100,vector<Entity*> (1, NULL)));

The error the compiler is giving is "expected identifier before numerical constant". I checked closely, and there are no missing semicolons or anything before this line. If I remove this line, the code compiles cleanly.

This is directly after the "private:" line in a function definition. Can member variables accept constructors? I also tried putting just

vector <vector <vector <Entity* > > > matrix;

in the .h file, and

matrix (100, vector < vector < Entity* > > (100,vector<Entity*> (1, NULL)));

in the constructor in the .cpp file, but that didn't work either (no match for call, and invalid conversion from int to Entity*)

What's the best way to declare a 3d "vector" of this (especially in a class?)

Thanks!

taocp
  • 23,276
  • 10
  • 49
  • 62
Nathan
  • 73,987
  • 14
  • 40
  • 69
  • Realize that you are going to get awful data locality due to the separate allocations. If performance becomes an issue, consider one large vector and manually calculating offsets. Also, prefer `std::unique_ptr` or `std::shared_ptr` to raw pointers if the vector "owns" each element. – Ed S. May 15 '13 at 22:55
  • Is there any memory issues with using a 2d array of vectors? – Nathan May 15 '13 at 22:59
  • It's not about how *much* memory you use, it's about *where* that memory is allocated. Each vector will allocate its own heap of memory, meaning that iterating over them will have you bounding around in memory (and thus incurring many cache misses). Still, make sure it is actually an issue for you first. – Ed S. May 15 '13 at 23:00
  • If I have a 2d array of vectors, is that handled in memory like a 2d array of pointers to heap-allocated memory? – Nathan May 15 '13 at 23:02
  • @Nathan no - each vector is a class with its own members and a bunch of memory allocated on the heap. Often it allocates more memory than it needs, but if you specify it in the constructor it should be the exact amount. But that means you need storage for the class members. It's not just pointers. – paddy May 15 '13 at 23:08
  • Okay, thanks. So the vectors are allocated on the stack, but their data is allocated on the heap? – Nathan May 15 '13 at 23:13
  • Why don't you use another class to represent the 3D vector? – eLRuLL May 16 '13 at 03:18

2 Answers2

2

You need to initialize it in your class constructor.

class MyClass
{
public:
    MyClass()
        : matrix (100, vector < vector < Entity* > > (100,vector<Entity*> (1, NULL)))
    {}

private:
    vector <vector <vector <Entity* > > > matrix;
};

Personally, I wouldn't use vectors-of-vectors for matrices, as I've discussed in the following post:

Get the first column of a matrix represented by a vector of vectors

I would recommend you take an approach similar to that, but adapted for a 3D matrix. Essentially you treat the first two dimensions as a rectangle (square, in your case), and you stack those rectangles to create the third dimension. If you wanted to resize the 3rd dimension to 10 elements, you would add 9 more chunks of 100x100. That would be a single vector resize operation.

If you wanted to do the above resize with vector-vector-vector, you would need to perform 10,000 vector resize operations. You would be spreading your element storage all over the heap, and getting terrible performance due to cache misses.

Community
  • 1
  • 1
paddy
  • 60,864
  • 6
  • 61
  • 103
  • I have inlined the constructor in this example (*ie* the definition is in the same place as the declaration). You can define the constructor in the CPP file instead like this: `MyClass::MyClass() : matrix( ... ) {}`. I've used `...` for brevity. In that case, the declaration in the class is simply: `MyClass();`. Maybe you should find an introductory tutorial on C++ – paddy May 15 '13 at 23:04
0

vector< vector< vector > > dp(m, vector< vector >(n , vector(l,1)));

Satakshi Pandey
  • 234
  • 4
  • 9
  • Even if you fix the formatting here, so that the syntax is not broken, ... – Yunnosch Oct 19 '20 at 18:49
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Oct 19 '20 at 18:49