Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: touchstone on January 15, 2004, 02:21 PM

Title: vector in C++
Post by: touchstone on January 15, 2004, 02:21 PM

vector<int> row;
vector< vector<int> > temp;
vector< vector< vector<int> > > matrix;

// initialize row
for (int x = 0; x < 10; ++ x)
row.push_back(x);
temp.push_back(row);
temp.push_back(row);
matrix.push_back(temp);



// how can i print this matrix???
do i need any STL display function????

is it possible to use typedef here?? how?

i wrote ....to see the content of the matrix


for(int i=0; i < matrix.size(); ++i) cout << matrix << endl;


but i am getting wrong garbage values....

how do i print the matrix??
Title: Re:vector in C++
Post by: Skywing on January 15, 2004, 03:36 PM

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;



vvi matrix;
/* initialize here ... */
for(int i = 0; i < matrix.size(); i++)
for(int j = 0; j < matrix[i].size(); j++)
 cout << "(" << i << ", " << j << ") == " << matrix[i][j] << endl;

Add more inner loops for each dimension/subscript.
Title: Re:vector in C++
Post by: touchstone on January 16, 2004, 06:40 AM
hi

its not working ... i wrote



#include<vector>
#include<stdio.h>

int main()


{
 typedef vector<int> row;
 typedef vector<row> temp;

 row R1;
 temp T1;

 for (int x = 0; x <3; ++ x)
 R1.push_back(x);
 T1.push_back(R1);
 T1.push_back(R1);
 matrix.push_back(T1);


for(int i = 0; i < matrix.size(); i++)
for(int j = 0; j < matrix[i].size(); j++)
printf("%d ", matrix[i][j]);


}




no its not working....giving compilation error
Title: Re:vector in C++
Post by: Kp on January 16, 2004, 09:43 AM
While I generally don't offer solutions to people who complain about compile errors and don't supply the error text, I will in this case.  You need to declare matrix if you plan on refering to it!