CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2011
    Posts
    5

    Question problem with 2D vector

    Hi all, i am new is C++.

    i want to create a global 2D matrix and here is the code is use:
    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    vector<vector<int> >mat; // 2D matrix
    
    void func(void){
         
         for(int i=0; i<5; i++)
                for(int j=0; j<5; j++)
                        mat[i][j] = 0;
         
    }
    
    int main(void){
        int size_x = 5; // X_dimension
        int size_y = 5; // Y_dimension
        
        mat.resize(size_x, vector<string>(size_y)); //resize 2D matrix
        
        for(int i=0; i<size_x; i++)
                for(int j=0; j<size_y; j++)
                        mat[i][j] = i;
        
        //print matrix
        for(int i=0; i<size_x; i++){
                cout << "\n";
                for(int j=0; j<size_y; j++){
                        cout << mat[i][j] << " ";
                }
        }
        
        func(); // create a zero matrix
         
        //print zero matrix
        cout << "\n\n";
        for(int i=0; i<size_x; i++){
                cout << "\n";
                for(int j=0; j<size_y; j++){
                        cout << mat[i][j] << " ";
                }
        }
        
        cout << "\n\n";
        system("PAUSE");
        return 0;
    }
    i am doing something wrong but i have no idea how to correct my mistake.

    Any help will be appreciated.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: problem with 2D vector

    Quote Originally Posted by msoh View Post
    i want to create a global 2D matrix and here is the code is use:
    Code:
    ....
    i am doing something wrong but i have no idea how to correct my mistake.
    So what goes wrong?
    Victor Nijegorodov

  3. #3
    Join Date
    May 2011
    Location
    India
    Posts
    18

    Re: problem with 2D vector

    Use { for .. for statements .. prob will be solved ..

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: problem with 2D vector

    How does that code compile ?

    Code:
    vector<vector<int> >mat; // 2D matrix
    
    // ...
    
    mat.resize(size_x, vector<string>(size_y)); //resize 2D matrix

  5. #5
    Join Date
    Jul 2011
    Posts
    5

    Re: problem with 2D vector

    Thanks all very much for the replies. I edited the first post.

  6. #6
    Join Date
    Jul 2011
    Posts
    5

    Re: problem with 2D vector

    Quote Originally Posted by msoh View Post
    Thanks all very much for the replies. I edited the first post.
    sorry but how can i edit my first post to correct my mistake. if this is not possible then the change i would do is what Philip Nicoletti wrote. Thanks again for the replies everyone.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured