Multi-Dimensional Vector

This is a discussion on Multi-Dimensional Vector within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble outputting a multi-dimensional vector that has specific values. Code: void DisplayVect (const vector<vector<int> >& v); int main(){ ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    19

    Multi-Dimensional Vector

    I'm having trouble outputting a multi-dimensional vector that has specific values.

    Code:
    void DisplayVect (const vector<vector<int> >& v);
    
    int main(){
    
    	const int Max = 7;
    
    	vector<vector<int> > v(8,5);
    
    	DisplayVect(v);
    
    	system("Pause");
    
    }
    
    void DisplayVect (const vector<vector<int> >& v)
    {  for (int i = 0; i < v.size(); i++)       // loops through each row of v
       {  for (int j = 0; j < v[i].size(); j++) // loops through each element of each row 
              cout << v[i][j] << " ";           // prints the jth element of the ith row
          cout << endl;
       }
    }
    That code above works fine, but as far as assigning values, this code will add on to the vector of 0's with values from 0 to 6, instead of changing the values that are already printed.
    Code:
    for(int i = 0; i < Max; i++){
    	for(int j = 0; j < Max; j++){
    		v[i].push_back(j);
    	}
    	}
    Thanks for your help.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    You are using the push_back() member function which was designed precisely to add on one more element to the container.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    push_back() adds new elements onto the vector. You should use assignment and subscripting like you did in the first loop:
    Code:
    void ResetVect (const vector<vector<int> >& v)
    {  for (int i = 0; i < v.size(); i++)       // loops through each row of v
       {  for (int j = 0; j < v[i].size(); j++) // loops through each element of each row 
              v[i][j] = 0;
       }
    }

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Thanks, that makes sense about the push_back() part.

    One problem with the code, in the ResetVect() function, an error occurs when v[i][j] = 0:
    error C3892: 'j' : you cannot assign to a variable that is const
    I'm not too sure why that is occurring.



    Code:
    void DisplayVect (const vector<vector<int> >& v);
    void ResetVect (const vector<vector<int> >& v);
    
    
    int main(){
    
    	const int Max = 8;
    
    	vector<vector<int> > v(8,5);
    
    	
    	ResetVect(v);
    	DisplayVect(v);
    	
    
    
    	system("Pause");
    
    }
    
    void DisplayVect (const vector<vector<int> >& v)
    {  for (int i = 0; i < v.size(); i++)       // loops through each row of v
       {  for (int j = 0; j < v[i].size(); j++) // loops through each element of each row 
              cout << v[i][j] << " ";           // prints the jth element of the ith row
          cout << endl;
       }
    } 
    
    void ResetVect (const vector<vector<int> >& v)
    {  for (int i = 0; i < v.size(); i++)       // loops through each row of v
       {  for (int j = 0; j < v[i].size(); j++) // loops through each element of each row 
              v[i][j] = 0;
       }
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If you are modifying the vector, the parameter should not be a reference to const, it should just be a reference.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    I'm still having some trouble with the program; it will output a 5x8 vector of 0's but that's about it.

    What am I missing in my code so that the values in each row will go from 0 to 4?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    What is your updated code snippet?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Forgot about that:

    Code:
    ...
    void DisplayVect (vector<vector<int> >& v);
    void ResetVect (vector<vector<int> >& v);
    
    
    int main(){
    
    	vector<vector<int> > v(8,5);
    
    
    	
    	ResetVect(v);
    	DisplayVect(v);
    	
    
    
    	system("Pause");
    
    }
    
    void DisplayVect (vector<vector<int> >& v)
    {  
    	for (int i = 0; i < v.size(); i++){       // loops through each row of v
    	   for (int j = 0; j < v[i].size(); j++){ // loops through each element of each row 
    	       cout << v[i][j] << " ";			 // prints the jth element of the ith row
    	   }
    	 cout << endl;
       }
    } 
    
    void ResetVect (vector<vector<int> >& v)
    {  
    	for (int i = 0; i < v.size(); i++){      // loops through each row of v
    		for (int j = 0; j < v[i].size(); j++){ // loops through each element of each row 
              v[i][j] = 0;
    		}
       }
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Well, it seems to me that you need to add another function along the lines of ResetVect() to perform this special assignment of numbers for you.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Kramer55 View Post
    I'm still having some trouble with the program; it will output a 5x8 vector of 0's but that's about it.

    What am I missing in my code so that the values in each row will go from 0 to 4?
    I'm mot shure that I understood the question ( because the answer is so obvious ).
    Code:
    void ResetVect (vector<vector<int> >& v)
    {  
    	for (int i = 0; i < v.size(); i++){      // loops through each row of v
    		for (int j = 0; j < v[i].size(); j++){ // loops through each element of each row 
              		v[i][j] = j;
    		}
       	}
    }
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 03:56 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  3. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 11:38 AM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 01:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21