C++ beginner's questions on vectors of vectors (declaration, definition)

Hello everyone:

I am a C++ beginner. I have questions regarding vectors of vectors. I have read the available threads on this topic and would very much appreciate it if someone could help with further questions. I apologize in advance for my ignorance!

Here is what I need to do: set up a finite element problem. Specifically, I need to (1) construct a 2-D grid (x and y points/nodes). Plus, (2) each node/point needs to have a vector associated with it (of physical properties like density, viscosity, etc).

Here is how I am thinking of doing it: use a 3-D vector. Two of the vectors would deal with the mesh/grid for the finite difference calculation and the third vector would contain the physical properties (density, etc). So question 1 is, does this sound like a good approach? If so, does anyone have a simple example of this?

Question 2: I would like to have a class called "Mesh" that will contain the data members x and y (for the nodes that make up the mesh). it will also contain physical properties. The thing is, as I continue with this project, I expect things to get complicated. So I would like to have 3 files: a header file where the Mesh class and its data members and member functions are declared, a .cpp file where the data members and member functions are defined, and a main file. The problem is, I cannot figure out 1) how to declare a 3-D vector in the header file and 2) I cannot figure out exactly how to define it in the .cpp definition file. I am fine with declaring and defining variables, but the 3D vector syntax is confusing me...

Specifically, in the header, would/could I have this:

Mesh(double myMeshRows, double myMeshColumns, double physProps)

where myMeshRows is the vector of x nodes, myMeshColumns is the vector of y nodes, and physProps is the vector of physical properties?

Then in the .cpp definition file, could I have this syntax:

Mesh::Mesh()
: id(someid), x(myMeshRows), .....??

Thank you.



I have the same stance on multi-D vectors as I do multi-D arrays: I hate them. (Well it's not that they're bad, they're just overused / misused)

A grid does not call for a 2D vector. It calls for a 1D array/vector. Layering vectors upon vectors upon vectors impedes performance and is unnecessary.

However... putting a vector in the node makes sense IF each node is going to have a different amount of properties. Since I find this a little puzzling... I'm not sure this is really what you need.

My approach would be to make a 'Properties' struct/class and have a 1D vector to it. Or if you don't like using 1D vectors for 2D lookup, you can wrap the vector in a class to access it 2D style.

Something like this:

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
template <typename T>
class Grid2D
{
public:
  T& operator () (int x,int y)
  {
    return mVec[ y*nWd + x ];
  }
  const T& operator () (int x,int y) const
  {
    return mVec[ y*nWd + x ];
  }

  void Resize( int width, int height )
  {
    // note this probably doesn't resize as you'd expect.  I'm too lazy to write a proper resize for
    // this basic example
    nWd = width;
    nHt = height;
    mVec.resize(nWd * nHt);
  }

private:
  int nWd;
  int nHt;
  std::vector<T>  mVec;
};

//------------------------------------

struct PROPERTIES
{
  double density;
  double foo;
  double bar;
};

//---------------------------------

int main()
{
  // make a 5x6 grid of PROPERTIES
  Grid2D<PROPERTIES> grid;
  grid.Resize(5,6);

  // set the density at x=3, y=1 to 0.5
  grid(3,1).density = 0.5;
}
Last edited on
Hi Disch:

Thanks a lot for your example and for your advice about using vectors of vectors!

It is going to take me a while to work through your example (not yet familiar with templates and operator keyword--good opportunity to learn!), but at first glance it looks like exactly what I need to get started. I hope you won't mind if I maybe post a few questions down the line... I'd like to develop good habits and I might want to ask you more about your comments on putting vectors on the nodes and using vectors of vectors in general.

Thanks again; really appreciate all the time you put in here.

Topic archived. No new replies allowed.