How to declare a Three-index vector

Can someone help me. I would like to declare a three-index vector. I already did a two-index one. As above:

1
2
vector <vector<double> > c; //cost
c = vector<vector <double> >(d.n,vector<double>(d.n + 1));


where d.n is the dimension of the index i and (d.n+1) of the index j.

Now I need to introduce a new index, called o. The dimension of o is (d.Q + 1).

Thanks a lot!

Do you really want a 3D vector?

Most folk who start off wanting one eventually realises that they can get away with a cheaper data structure,
Hello kbw,

What kind of cheaper data structure? If you provide me someone better I could change. But how can I declare a three-index vector?

Thanks
it depends on what you're doing. Can you explain why you need it?
Last edited on
If a vector is 1D, and
a vector of vectors is 2D,
then what would a 3D be?

A vector of vectors of vectors?


However, you could just take a 1D vector and wrap it into a class that provides a method that returns a reference to an element, when you call it with the three indices.
I think keskiverto may be right. If you know the dimensions ahead of time and they won't be changing during the lifetime of the array, then you may be better off writing this yourself:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Array3D {
public:
    Array3D(int I, int J, int K);
    vector<double> data;
    double &operator()(int I, int J, int K);
    int i, j, k;
};


Array3D::Array3D(int I, int J, int K) :
    i(I), j(J), k(K), data(I*J*K)
{;}

double &
Array3D::operator()(int I, int J, int K)
{
    if (I<0 || I >= i) {
	cerr << "i out of bounds\n";
	abort();
    }
    if (J<0 || J >= j) {
	cerr << "j out of bounds\n";
	abort();
    }
    if (K<0 || K >= k) {
	cerr << "k out of bounds\n";
	abort();
    }
    return data[I*j*k + J*k + K];
}

int main(int argc, char *argv[])
{
    Array3D arr(3,4,5);
    int i,j,k;
    int x=0;
    for (i=0; i<3; ++i) {
	for (j=0; j<4; ++j) {
	    for (k=0; k<5; ++k) {
		arr(i,j,k) = ++x;
	    }
	}
    }

    for (i=0; i<3; ++i) {
	for (j=0; j<4; ++j) {
	    for (k=0; k<5; ++k) {
		cout << arr(i,j,k) << ' ';
	    }
	    cout << '\n';
	}
    }
    return 0;
}


Output:
1 2 3 4 5 
6 7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 
21 22 23 24 25 
26 27 28 29 30 
31 32 33 34 35 
36 37 38 39 40 
41 42 43 44 45 
46 47 48 49 50 
51 52 53 54 55 
56 57 58 59 60 


This example does range checking on the array arguments. Note that it uses arr(i,j,k) to access an element instead of arr[i][j][k]. You could make the latter work but it would require a couple of helper classes.
The question isn't really how to make a 3D array, that's easy.

The question is do you really want a data structure with O(n*n) search characteristics, O(n*n) insert characteristics if it has to grow, and an increasingly large memory requirement for possibly sparse data ... clear not. So in order to find something better, you need to understand the requirement.
Topic archived. No new replies allowed.