I am asked to create a class that contains a 3D table. However, the size is not standard and are given by the user. I can obviously use a vector, and that would be fine. However, can I make the vector of constant size after initializing it, or is there an alternative to create such an object, without the use of a vector? Thanks in advance
Asked
Active
Viewed 72 times
0
-
2Wrap a 1d vector in your own class so you can control the vector and make sure it's size isn't changed. You can kind of see how to do that here: https://stackoverflow.com/questions/43358369/c-n-nested-vectors-at-runtime/43358434#43358434 – NathanOliver Mar 24 '21 at 21:30
-
There is a difference between *a class that contains a 3D table* and *a class that represents a 3D table*. Make sure you know what you are trying to implement. – R Sahu Mar 24 '21 at 21:35
-
Instead of a vector you can use a 3d linked list if you are fancy - prob. would be even fun to do :) Or basically any container you want some even holding a struct. – Ingo Mi Mar 24 '21 at 21:35
-
You can do something like : ` yourType *3DTable = new yourType [size][size][size]; ` – TUI lover Mar 24 '21 at 21:39
-
@RSahu No actually it contains a 3d table of other objects. Is like a cube of smaller cubes that has in it other objects. – Georgios Demeteiou Mar 24 '21 at 21:40
-
@Ivanovic unfortunately formally i have not be taught lists, can you elaborate a bit on the second proposal – Georgios Demeteiou Mar 24 '21 at 21:41
-
@TUIlover No, they can't. The size of array element must be compile time constant, even if the array is dynamic. Also, identifiers cannot start with a number. – eerorika Mar 24 '21 at 21:43
-
@TUIlover the point is that size are arbitrary variables determined by the user – Georgios Demeteiou Mar 24 '21 at 21:43
-
1@GeorgiosDemeteiou It is basically a struct "container" holding the pointer of the next struct and you are filling your resources dynamically. (A cascade of structs or classes) 3D just means holding 3 different valaues (value of a value and value of a value of a value) [Alex Allain had the best explaination for me](https://www.cprogramming.com/tutorial/lesson15.html) – Ingo Mi Mar 24 '21 at 21:46
-
@eerorika size of stl arrays must be constant, but the key word new dynamically allocate memory – TUI lover Mar 24 '21 at 22:13
-
@TUIlover I don't know what you mean by "stl arrays" but as I said: The size of array element must be compile time constant, **even if the array is dynamic**. – eerorika Mar 24 '21 at 22:14
-
I didn’t understand what you meant at first place but now I do : int *a = new int[n]; is valid, int ** a = new int[n][n]; isn’t – TUI lover Mar 24 '21 at 22:24
2 Answers
2
However, can I make the vector of constant size after initializing it
If you don't resize the vector, then its size remains constant. You can make the vector a private member of a class to enforce such restriction with a class invariant.
or is there an alternative to create such an object, without the use of a vector?
You can allocate a dynamic array without using a vector. But it won't be quite as convenient.

eerorika
- 232,697
- 12
- 197
- 326
-
1It is exactly the case, i was wondering if there is an alternative, but in any case thank you for the immediate response – Georgios Demeteiou Mar 24 '21 at 21:42
-
I think it is just the first part of the question, the answer is missing "is there an alternative to create such an object, without the use of a vector" – Ingo Mi Mar 24 '21 at 21:50
-
1
-
Wouldn’t an unordered map where keys are arrays of size 3 be a solution? – TUI lover Mar 24 '21 at 22:31
-
@TUIlover Well, array cannot be key of a map, but it can work if you wrap it in a class (i.e. use `std::array`). However, if every index has an element, then that would be a very inefficient solution. It could be OK if intention is for elements to exist only in small fraction of indices. – eerorika Mar 24 '21 at 23:19
0
You can by using new
, but it means a lot of work on your side. Here is a minimal example :
class threeDTable {
public :
threeDTable(int n){data = new yourType[n*n*n]; size=n;}
~threeDTable(){delete []data;}
void set(int x, int y, int z, yourType value){data[size*size*x + size*y + z]=value;}
yourType get(int x, int y, int z)const {return data[size*size*x + size*y + z];}
private :
yourType *data;
int size;
};

TUI lover
- 542
- 4
- 16