Jonathan Prieto-Cubides
Jonathan Prieto-Cubides

Reputation: 2847

three dimensional vectors in C++

I have in my c++ code:

  typedef vector<int> cards;
  typedef vector<cards> rows;
  typedef vector<rows> matriz;

and in my int main() , i try to initilizate a matriz called "cartas" with this line;

  63  cin>>n>>m;
  66  cartas(n,rows(m, cards(0)));

but, with g++, get out this error:

flip.cpp: In function ‘int main()’:
flip.cpp:66: error: no match for call to ‘(matriz) (int&, rows)’

i want to take a matriz of n*m, where in each position, there is vector of integers.

thanks, now, i don't see it how.

Upvotes: 1

Views: 898

Answers (2)

Luchian Grigore
Luchian Grigore

Reputation: 258618

Is your code by any chance similar to:

int n, m;
matriz cartas;
cin>>n>>m;
cartas(n,rows(m, cards(0)));

?

That won't work, matriz cartas; is already an initialization. Either define cartaz after the cin statement, or assign afterwards.

Optimal:

int n, m;
cin>>n>>m;
matriz cartas(n,rows(m, cards(0)));

Alternative:

int n, m;
matriz cartas;
cin>>n>>m;
cartas = matriz(n,rows(m, cards(0)));

Upvotes: 4

Horonchik
Horonchik

Reputation: 477

Why not just set an array of size n*m vector[]. you can get each row as index % n and column as index % m. Much simpler and cleaner.

Upvotes: 0

Related Questions