Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compilation error

Status
Not open for further replies.

satellite03

IS-IT--Management
Dec 26, 2003
248
IN
hi i am trying to learn using vector of a vector
thats why i wrote the code below but unfortunately its not compiling( and also have doubt whether it will run )

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


{
  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(&quot;%d &quot;, matrix[i][j]);


}

 


i have also doubt that whether my matrix[i][j] will be printed in this way.....anyway let me first remove compilation error...
 
int main() is also there...i missed it to post.

int main()
{

//

}
 
Firstly, put :

using std::vector;

below your #include directives. STL classes are defined in namespace std.

Secondly, what is the type of the matrix entity?


--
Globos
 
ok....after using namespace stll i am not getting sucesses
i wrote
--------
Code:
#include<vector>
#include<stdio.h>
using namespace std;

int main()

{
  typedef vector<int> row;
  typedef vector<row > temp;
  typedef vector<temp> matrix;
  row R1;
  temp T1;
  matrix M1;


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

for(int i = 0; i < M1.size(); i++)
for(int j = 0; j < M1[i].size(); j++)
printf(&quot;%d &quot;, M1[i][j]);
}

 g++ x.cpp
x.cpp: In function `int main ()':
x.cpp:24: warning: cannot pass objects of non-POD type `class
vector<int, allocator<int> >' through `...'


./a.out

-1073743440 -1073743440


in fact i wanted to see the content of matrix .....how do i see the content of a vector of vector of vector????

 
This will display the content of your matrix :

for(int i = 0; i < M1.size(); i++)
for(int j = 0; j < M1.size(); j++)
for(int k = 0; k < M1[j].size(); k++)
printf(&quot;%d &quot;, M1[j][k]);

/JOlesen
 
Ooops ....
for(int i = 0; i < M1.size(); i++)
for(int j = 0; j < M1.size(); j++)
for(int k = 0; k < M1[j].size(); k++)
printf(&quot;%d &quot;, M1[j][k]);
 
Why are you using C style output (printf) instead of C++ output (cout)? Anyway, your output code woud work for a vector of vectors, but matrix is a vector of vectors of vectors (3 dimensions) so you need one more for loop and array subscript, like this:
Code:
#include<vector>
#include<iostream>
using namespace std;

int main()
{
  typedef vector<int> row;
  typedef vector<row > temp;
  typedef vector<temp> matrix;
  row R1;
  temp T1;
  matrix M1;


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

  for(int i = 0; i < M1.size(); i++)
    for(int j = 0; j < M1[i].size(); j++)
      for(int k = 0; k < M1[i][j].size(); k++)
        cout << M1[i][j][k];
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top