Multidimensional vector array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rottmanj
    New Member
    • Jan 2008
    • 46

    Multidimensional vector array

    I am trying to build a multidimensiona l vector array that allows for multiple child elements under each parent element, but I am having a heck of a time trying to figure out how to do it.

    Right now with the code posted below. I get this type of output.

    Code:
    Resource : 0
    Resource : 1
    Resource : 2
    But this is more along the lines of what I am trying to accomplish.
    Code:
    Resource : 0
    	Class: 0
    	Class: 1
    Resource : 1
    	Class: 0
    	Class: 1
    Resource : 2
    	Class: 0
    	Class: 1
    Everything I have tried doesn't end with this result. So I am at a bit of a loss as what I am doing wrong.

    Here is my current code implementation (the child array elements have been removed so I could test some other ideas). Any help with this is greatly appreciated.

    Code:
    	vector< vector<int> > vec;
    
    	for (int i = 0; i < 3; i++) {
    	    vector<int> row;
    	        row.push_back(i);
    	    vec.push_back(row);
    	}
    	
    	for(int k = 0; k < vec.size(); k++){
    		for(int l = 0; l < vec[k].size(); l++){
    			cout << "Resource : " << vec[k][l] << endl;
    		}
    	}
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    I'm not completely sure what you're trying to do, but my guess is that for each row, you want to output the row number, and then all the elements in that row. Here is code that does that:
    Code:
    for(int k = 0; k < vec.size(); k++){
        cout << "Row " << k << endl;
        for(int l = 0; l < vec[k].size(); l++){
            cout << vec[k][l] << endl;
        }
    }
    I hope it's helpful.

    Comment

    • rottmanj
      New Member
      • Jan 2008
      • 46

      #3
      Well that part I have. What I am trying to do is populate the array.

      Lets say that Row is the parent element in the array (vec[0][0]), and I wanted to populate the child element of the parent array (vec[0][0][0]). I am not sure how to populate the child element of the array.

      More or less I am trying to create a 3D vector array.

      Comment

      • rottmanj
        New Member
        • Jan 2008
        • 46

        #4
        Here is a really good example of what I am trying to accomplish.



        Where each row would be the parent element and each column would be the child element of the parent.

        Comment

        • boxfish
          Recognized Expert Contributor
          • Mar 2008
          • 469

          #5
          Okay, how about this?
          Code:
          for (int i = 0; i < 3; i++) {
              vector<int> row;
              for (int j = 0; j < 3; j++)
                  row.push_back(i);
              vec.push_back(row);
          }
          Edit:
          Or if you want 3D:
          Code:
          for (int i = 0; i < 3; i++) {
              vector<int> level;
              for (int j = 0; j < 3; j++) {
                  vector<int> row;
                  for (int k = 0; k < 3; k++)
                      row.push_back(i);
                  level.push_back(row);
              }
              vec.push_back(level);
          }

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            boxfish is right. In a 2D array, you have to use 2 for...loops to access the array (well, as long as you access it as a 2D array...weaknes sforcats has a great article about this type of nuance in the insights section called Arrays Revealed), so it is expected that you should need 2 for...loops to access every element in a (simulated) 2D vector.

            Comment

            Working...