Dynamic Array Allocation in C ++

2

I would like to allocate this array dynamically in C ++:

int main()
{
    int n,m;
    cin>>n>>m;


   if(n>m){cedula tabela[n][n];}
   else {cedula tabela[m][m];} return 0;}

But the code does not compile, I think it has to do with dynamic allocation, I tried to do the following:

int main()
{
    int n,m;
    cin>>n>>m;


   if(n>m){cedula* tabela= new cedula[n][n];}
   else {cedula* tabela = new cedula[m][m];} return 0;}

But it did not compile anyway.

    
asked by anonymous 13.02.2017 / 13:29

1 answer

2

If you are programming in C ++, use vector with two dimensions. The old array should be avoided in C ++. Other structures can be useful too .

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    n = max(n, m);
    vector<vector<int>> cedula(n, std::vector<int>(n));
}

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

    
13.02.2017 / 13:45