Foros del Web

Foros del Web (http://www.forosdelweb.com/)
-   C/C++ (http://www.forosdelweb.com/f96/)
-   -   Matriz de dos dimenciones Variables (http://www.forosdelweb.com/f96/matriz-dos-dimenciones-variables-534764/)

spike886 20/11/2007 23:12

Matriz de dos dimenciones Variables
 
Lo que yo quiero hacer es una matriz dinamica de dos dimenciones, dadas en tiempo de compilacion.

Se que esto no se puede por que la segunda deve ser una constante

int** matriz=new int[x][y];

Me dijeron que tenia que hacer esto:

int** matriz= new int*[x];
for(i=0;i<x;i++)
matriz[i]=new int[y];


Pero cuando ejecuto la ultima linea se me cierra el programa

¿como lo deveria hacer? o ¿que estoy haciendo mal?

r0d 21/11/2007 08:34

Re: Matriz de dos dimenciones Variables
 
Hola,

¿programas en C o C++? Porque si es C++, el mejor es de utilizar la clase vector de la STL.

Pero, en embos langages, el mejor es de utilizar un array de 1 dimension.

Y si programas en c++, ya tengo una clase que hace (creo) lo que quieres:
Código:

template <typename T>
class Dyn2DMatrix
{
private:
    size_t m_nbRow, m_nbCol;
    std::vector<T> m_array;
    T m_neutralElement; 
 
public:
    // --- construction/destruction 
    Dyn2DMatrix() 
    : m_nbRow(0)
    , m_nbCol(0)
    , m_neutralElement(T()) 
    { }
 
    Dyn2DMatrix(const size_t nbRow, const size_t nbCol) 
    : m_nbRow(nbRow)
    , m_nbCol(nbCol)
    , m_neutralElement(T()) 
    { 
          std::vector<T>::iterator it = m_array.begin();
          m_array.insert(it, nbRow*nbCol, m_neutralElement);
    } 
 
    Dyn2DMatrix(const T &neutralElement) 
    : m_nbRow(0)
    , m_nbCol(0), m_neutralElement(T()) 
    { } 
 
    Dyn2DMatrix(const size_t nbRow, const size_t nbCol, const T &neutralElement) 
    : m_nbRow(nbRow)
    , m_nbCol(nbCol)
    , m_neutralElement(neutralElement) 
    { 
          std::vector<T>::iterator it = m_array.begin();
          m_array.insert(it, nbRow*nbCol, m_neutralElement);
    } 
 
    ~Dyn2DMatrix(){} 
 
    // --- accesseurs 
    T operator() (const size_t row, const size_t col) const 
    { 
          if ( ( m_nbRow < row ) || ( m_nbCol < col ) ) 
              throw new std::exception("const Dyn2DMatrix subscript out of bounds"); 
 
          return m_array[m_nbCol*row+col];
    } 
 
    size_t width() {return m_nbCol;} 
    size_t height() {return m_nbRow;} 
 
    // --- mutateurs 
    T &operator() (const size_t row, const size_t col) 
    { 
          // Dans un premier temps, je vérifie si la matrice est assez grande. 
          // Si elle ne l'est pas, je l'aggrandis. 
          if ( m_nbCol <= col ) 
          {
              // je dois ajouter des colonnes 
              size_t colDiff = col - m_nbCol + 1;
              for (size_t curRow = 0 ; curRow < m_nbRow ; ++curRow ) 
              { 
                    std::vector<T>::iterator it = m_array.begin();
                    size_t tmp = curRow*(col+1) + m_nbCol;
                    m_array.insert( it + tmp, colDiff, m_neutralElement);
              } 
              m_nbCol = col+1;
          } 
 
          if ( m_nbRow <= row ) 
          {
              // je dois ajouter des rangées 
              std::vector<T>::iterator it = m_array.begin();
              m_array.insert(it+m_nbCol*m_nbRow, (row - m_nbRow + 1)*m_nbCol, m_neutralElement);
              m_nbRow = row+1;
          } 
 
          // puis je retourne simplement l'élément demandé. 
          return m_array[m_nbCol*row+col];
    } 
 
    // méthode clear: initialise tous les éléments de la matrice avec l'élément passé 
    // en paramètre. 
    void clear(const T &initElement = m_neutralElement) 
    { 
          std::vector<T>::iterator it;
          for ( it = m_array.begin() ; it != m_array.end() ; ++it) 
              (*it) = initElement;
    }
};

*perdón para mis faltas, no hablo todavía muy bien español

spike886 21/11/2007 16:36

Re: Matriz de dos dimenciones Variables
 
Muchas gracias Che.
Es que me esoty iniciando en C++ y no comosia todo lo que tiene la std. Pero gracias a vos ya abri lo ojos:stress:


La zona horaria es GMT -6. Ahora son las 20:46.

Desarrollado por vBulletin® Versión 3.8.7
Derechos de Autor ©2000 - 2026, Jelsoft Enterprises Ltd.