4-D data type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TiggiePurr
    New Member
    • Mar 2007
    • 4

    4-D data type

    Thank goodness for websites like this one....

    I've been out of the C++ loop for about 10 yrs - so many changes (and things I've forgotten).

    It seems that the new "vector" type may be what I want.

    I need to declare a 4 dimensional array (user specified length and to be read from file). So far, I'd like to be able to use the vector functions for manipulating and storing.

    Assuming numx, numy, mx and my represent the dimensions for f I need to figure out how to make the syntax work for the addition line which I know is incorrect. Below is the basic idea (using the standard namespace here...):

    void my_func(int numx, int numy, int mx, int my,
    vector< vector< vector< int > > > & f){

    for(i=0;i<mx;i+ +)
    for (j=0; j<my;j++)
    for(k=0;k<numx; k++)
    for(m=0;m<numy; m++)
    f[i][j][k][m] = i + j + k + m;
    }

    Also, I know when declaring

    vector<int> v(5);

    will create 5 elements in the vector.

    In this case is it possible to declare all 4 of the dims at the same time??

    And then take this a step further initialize them as well like you would when doing

    vector<int> v(5,1);

    to initialize all 5 elements to the value of 1?

    Your help is most greatly appreciated.
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you declared a 3D array and forgot to define the for() loop control variables
    the following should compile OK
    Code:
    #include <vector>
    using namespace std;
    
    void my_func(int numx, int numy, int mx, int my,
    vector< vector< vector <vector< int > > > > & f){
    int i, j, k, m;
    for(i=0;i<mx;i++)
    for (j=0; j<my;j++)
    for(k=0;k<numx;k++)
    for(m=0;m<numy;m++)
    f[i][j][k][m] = i + j + k + m;
    }

    Comment

    • TiggiePurr
      New Member
      • Mar 2007
      • 4

      #3
      Originally posted by horace1
      you declared a 3D array and forgot to define the for() loop control variables
      the following should compile OK
      Code:
      #include <vector>
      using namespace std;
      
      void my_func(int numx, int numy, int mx, int my,
      vector< vector< vector <vector< int > > > > & f){
      int i, j, k, m;
      for(i=0;i<mx;i++)
      for (j=0; j<my;j++)
      for(k=0;k<numx;k++)
      for(m=0;m<numy;m++)
      f[i][j][k][m] = i + j + k + m;
      }

      Sorry that was my fault when typing this into this site as I stripped the function down to this simple one - yes the variables have been declared. I'm receiving the pointer-to-object error which was because I neglected to add the fourth vector declaration. What about global declaration and initialization - any thoughts? I can do this via for loops, but this is for a fluid flow problem and is labor intensive and needs to be done many times. THANKS!!!

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Avoid global declaration like the plague:
        1) your global variable can be hidden by a local variable that has the same name. Therefore, you can't guarantee your global variable will be used.
        2) your global variable name may be duplicated by another global variable from someone else's code. You now die in redefinition errors
        3) your global variable is visible to all functions. That means a multithreaded program could have multiple functions simulatineously accessing rthe variable. This could cause a race condition and crash the program
        4) When your global variable gets screwed up, any of your functions coukld have done it. You have maximized the pool of suspects.

        I recommend:
        1) Bury the vector in a class
        2) Use a Singleton design pattern to guarantee only one instance of the class
        3) Bury the Singleton object in an anonymous namespace.
        4) Use a Singleton Registry

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          Originally posted by TiggiePurr
          Thank goodness for websites like this one....


          Also, I know when declaring

          vector<int> v(5);

          will create 5 elements in the vector.

          In this case is it possible to declare all 4 of the dims at the same time??

          And then take this a step further initialize them as well like you would when doing

          vector<int> v(5,1);

          to initialize all 5 elements to the value of 1?

          Your help is most greatly appreciated.
          try this - defines a 10*10*10*10 double vector elements initialised to 1?
          Code:
            vector < vector < vector < vector < double > > > >
              table(10, vector < vector < vector < double > > > 
                    (10, vector< vector<double> >(10, vector<double>(10, 1))));

          Comment

          • TiggiePurr
            New Member
            • Mar 2007
            • 4

            #6
            Originally posted by horace1
            try this - defines a 10*10*10*10 double vector elements initialised to 1?
            Code:
              vector < vector < vector < vector < double > > > >
                table(10, vector < vector < vector < double > > > 
                      (10, vector< vector<double> >(10, vector<double>(10, 1))));
            EXCELLENT!!! Thanks a ton!!

            Comment

            Working...