Valhalla Legends Forums Archive | C/C++ Programming | vector in C++

AuthorMessageTime
touchstone
[code]
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);
[/code]


// 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[i] << endl;


but i am getting wrong garbage values....

how do i print the matrix??
January 15, 2004, 8:21 PM
Skywing
[code]
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
[/code]

[code]
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;
[/code]
Add more inner loops for each dimension/subscript.
January 15, 2004, 9:36 PM
touchstone
hi

its not working ... i wrote

[code]

#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]);


}

[/code]


no its not working....giving compilation error
January 16, 2004, 12:40 PM
Kp
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 [u]matrix[/u] if you plan on refering to it!
January 16, 2004, 3:43 PM

Search