|
-
July 14th, 2011, 02:37 AM
#1
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.
-
July 14th, 2011, 02:44 AM
#2
Re: problem with 2D vector
 Originally Posted by msoh
i want to create a global 2D matrix and here is the code is use:
i am doing something wrong but i have no idea how to correct my mistake.
So what goes wrong?
Victor Nijegorodov
-
July 14th, 2011, 05:35 AM
#3
Re: problem with 2D vector
Use { for .. for statements .. prob will be solved ..
-
July 14th, 2011, 05:50 AM
#4
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
-
July 14th, 2011, 07:10 AM
#5
Re: problem with 2D vector
Thanks all very much for the replies. I edited the first post.
-
July 14th, 2011, 07:14 AM
#6
Re: problem with 2D vector
 Originally Posted by msoh
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|