Vector with 3 dimensions

Question

• Can you create vectors with dimensions greater then 2?

I had a static array that was

RNOutput[30][360][2];

I was going to declare a vector with

vector<vector<vector<double>>> RNOutput;

code...

RNOutput.resize(30,360,vector<double>(2))); //error no overloaded function takes 3 arguments

Thanks

Monday, August 23, 2010 8:46 PM

• `RNOutput.resize(30, vector<vector<double> >(360, vector<double>(2)));`

Some typedefs would improve the readability of this code considerably in my opinion...

```typedef vector<double> inner_vec_t;
typedef vector<inner_vec_t> middle_vec_t;
typedef vector<middle_vec_t> outer_vec_t;
...
outer_vec_t RNOutput;
...
RNOutput.resize(30, middle_vec_t(360, inner_vec_t(2)));```
• Marked as answer by Wednesday, August 25, 2010 4:10 PM
Monday, August 23, 2010 8:52 PM

All replies

• `RNOutput.resize(30, vector<vector<double> >(360, vector<double>(2)));`

Some typedefs would improve the readability of this code considerably in my opinion...

```typedef vector<double> inner_vec_t;
typedef vector<inner_vec_t> middle_vec_t;
typedef vector<middle_vec_t> outer_vec_t;
...
outer_vec_t RNOutput;
...
RNOutput.resize(30, middle_vec_t(360, inner_vec_t(2)));```
• Marked as answer by Wednesday, August 25, 2010 4:10 PM
Monday, August 23, 2010 8:52 PM
• Which dimensions are fixed? All of them? None of them? Something in between?
Monday, August 23, 2010 8:52 PM
• Ildjar - I have tried using typedefs maybe it's because I don't really get the code but they really confuse me.

The middle vector is not fixed

RNOutput[30][360 ][2];

does it matter?

Monday, August 23, 2010 10:43 PM
• does it matter?

Yes, it does. If all the dimensions are fixed, then you could just stick with a simple multidimensional array rather than a vector. If the left-most dimension was variable, then you could define a vector of a two dimensional array. As it stands, I think a vector of a vector of a vector probably would work best in this scenario.

By the way, did ildjarn's suggestion work for you? The code looks good to me.

Monday, August 23, 2010 11:16 PM
• I should make the OP aware that 3D like objects can use a lot of memory. Especially when the indexes move from 3 to 4 digits.

My tensor.h project has such objects and I had to use memory checks to make sure a call did not blow up the system.

Vote if answered or helpful, I am running for Office (joke)! IT/Developer, Windows/Linux/Mainframe I also am a true vegan and I am very good with economics and I used to play chess at 2400++ I have lots of papers on my site for power supplies and video card problems, see the resources section
Tuesday, August 24, 2010 2:22 AM
• Brian Muth [MVP] wrote:

If the left-most dimension was variable, then you could define a  vector of a two dimensional array.

Arrays are not copyable. You can't use one as an element in a vector.

Igor Tandetnik

Wednesday, August 25, 2010 2:07 AM
• You can if it's std::array or std::tr1::array
Wednesday, August 25, 2010 3:17 AM
• If I have a vector

RNOutput[100][100][100] this would be the same size as

RNOutput[100 * 100 * 100] = RNOutput[1000000];

Where 100 is the number of elements Right?

Wednesday, August 25, 2010 4:17 PM
• For an array (C-array or std::array) those would be the same size.

For vectors those are not the same size, since each vector instance has some overhead in addition to the elements it contains. For example, if the additional overhead of a vector is 12 bytes, and taking into account that a double is 8 bytes, the overall size of a vector<vector<vector<double>>> where each dimension has a size of 100 is 8121212 bytes; the size of a vector<double> with 1000000 elements would be 8000012 bytes.

• Edited by Wednesday, August 25, 2010 4:41 PM typo
Wednesday, August 25, 2010 4:36 PM
• If I have a vector

RNOutput[100][100][100] this would be the same size as

RNOutput[100 * 100 * 100] = RNOutput[1000000];

Where 100 is the number of elements Right?

That's correct, except your terminology is loose. RNOutput[100][100][100] is a multidimensional array, not a vector. 100 is the size of each dimension, and RNOutput[1][33][99] is an example of an element within that multidimensional array.

addendum: Upon reviewing this thread, I probably mis-read your post and realize you were talking about the vector<vector<vector<double>>> implementation, in which case ildjarn's post is entirely relevant: the amount of space allocated is not the same.
Wednesday, August 25, 2010 4:37 PM
• When I made my template library, I went to the trouble of making them fully assignable.

Using the old style arrays means the OP will need 3 deep for loops

Vote if answered or helpful, I am running for Office (joke)! IT/Developer, Windows/Linux/Mainframe I also am a true vegan and I am very good with economics and I used to play chess at 2400++ I have lots of papers on my site for power supplies and video card problems, see the resources section
Wednesday, August 25, 2010 9:56 PM
• No one is suggesting old style arrays. The two options here in my opinion are std::array<> or std::vector<>, both of which are intrinsically assignable without any extra effort.
Wednesday, August 25, 2010 10:16 PM
• When I checked this over, I saw many uses of the old array [] use which suggested the OP might be tempted to use it.

Best is

```std::vector<std::vector<std::vector<any_class>>> cuboid;
```

Vote if answered or helpful, I am running for Office (joke)! IT/Developer, Windows/Linux/Mainframe I also am a true vegan and I am very good with economics and I used to play chess at 2400++ I have lots of papers on my site for power supplies and video card problems, see the resources section
Wednesday, August 25, 2010 10:19 PM