Re: Multi-Dimension Vectors
Quote:
Originally Posted by
hfbroady
What I need to do is compare the first element (days) and when it is greater then or equal to 4
First, you can clean up your code:
Code:
#include <vector>
typedef std::vector<double> Double1D;
typedef std::vector<Double1D> Double2D;
typedef std::vector<Double2D> Double3D;
#define HEIGHT 5
#define WIDTH 3
#define DEPTH 7
int main()
{
Double3D array3D(HEIGHT, Double2D(WIDTH, Double1D(DEPTH)));
//..
That entire code replaces all of those loops. Also note the typedefs to clean up the template code.
Quote:
The rows of my 3d vector will be the days, the colums will be the serial numbers the depth will be the reading at the different temps.
There are no "rows" or "columns" or "depths". A vector within a vector is whatever you want it to denote. Someone may interpret
As "x" being the depth, and y,z being the rows and columns.
So which dimension of the 3d vector is the "row", which one is the "column" and which one is the "depth"? Also, what does "comparing the first element" mean? You have a vector with 3 indices, so which one is the first?
Code:
while( array3D[1][0][0] == 1)
Is this the first element? Arrays and vectors start at index 0, not 1.
Code:
while( array3D[0][0][0] == 1)
That would make a little more sense in comparing the "first element".
Regards,
Paul McKenzie
Re: Multi-Dimension Vectors
I cleaned the code up a bit. The first element that I'm talking about well i guess its not an element because its a vector inside vector inside a vector. So what I'm trying to do is comapre the values in the first vector only.... example array3d[1][0][0]. The vector witht the one in it is have I want to compare to see if it is greater than equal to 4?
Re: Multi-Dimension Vectors
Quote:
Originally Posted by
hfbroady
I cleaned the code up a bit. The first element that I'm talking about well i guess its not an element because its a vector inside vector inside a vector. So what I'm trying to do is comapre the values in the first vector only.... example array3d[1][0][0]. The vector witht the one in it is have I want to compare to see if it is greater than equal to 4?
That is the second vector, not the first. The one I posted is the actual first vector.
Also, the issue really has nothing to do with vector -- it has everything to do with how you are interpreting a 3-dimensional array. If you can't "picture" the 3-d array in your mind, then you aren't going to understand what each index denotes.
It is the rightmost index that actually has the individual data item. The second index is a "pointer" to the vector that contains the data items. The leftmost index is a pointer to the pointer to the data items. So
is the first double in the innermost vector, the middle 0 is the first vector of these items, and the leftmost 0 is the first vector of the middle set of vectors.
Why not start with the simple example I posted? Fill it with values, and then write a small program to figure out where things are.
Also, you could simplify all of this if you used iterators and the typedefs I had in my example.
Code:
#include <vector>
typedef std::vector<double> Double1D;
typedef std::vector<Double1D> Double2D;
typedef std::vector<Double2D> Double3D;
#define HEIGHT 5
#define WIDTH 3
#define DEPTH 7
int main()
{
Double3D array3D(HEIGHT, Double2D(WIDTH, Double1D(DEPTH)));
// Declare an iterator that points to the first innermost element
Double1D::iterator it = array3D[0][0].begin();
// loop through this vector
while (it != array3D[0][0].end())
{
*it = 10.0;
++it;
}
// Declare an iterator that points to the second middle vector, and points to the first innermost element
iterator it = array3D[0][1].begin();
// loop through this vector
while (it != array3D[0][1].end())
{
*it = 20.0;
++it;
}
So when you run this program (preferably in a debugger), do you see where the 10's and 20's are located?
Regards,
Paul McKenzie
Re: Multi-Dimension Vectors
Let's just go back 1 dimension for now, to make things simpler:
Code:
#include <vector>
typedef std::vector<double> Double1D;
typedef std::vector<Double1D> Double2D;
#define HEIGHT 5
#define WIDTH 3
int main()
{
Double2D array2D(HEIGHT, Double1D(WIDTH, 0));
//This is the first "row":
Double1D& first_row = array2D[0];
//This is the second "row":
Double1D& second_row = array2D[1];
//This is the first element of the second row:
double a = array2D[1][0];
//double a = second_row[1]; //equivalent
}
In reality there is no concept of "rows" or "columns", but merely order of indexing. In this case, we have a vector holding HEIGHT amount of vectors being WIDTH large.
Nothing stops you from indexing the other way around, as long as you are homogenous.
Personally, I like sticking to the matrix scheme
Re: Multi-Dimension Vectors
Quote:
Originally Posted by
hfbroady
I'm using a 3d vector to do this.
Multi-dimensional arrays are so BASICish. :)
In C++ it's more common to store compound data in structs or classes, like say
Code:
struct Measure {
long serialNo;
int days;
vector<double> data;
};
Then you can store Measure objects in an ordinary one-dimensional vector or maybe a map (with serial number as key).
Re: Multi-Dimension Vectors
Re: Multi-Dimension Vectors
On a side note, The Boost Multidimensional Array Library may provides much more and really easy to use.