Retroceder   Foros del Web > Temas generales de computación > Programación > C/C++

Respuesta
 
Herramientas Desplegado
Antiguo 20-nov-2007, 22:12   #1 (permalink)
spike886 ha deshabilitado el karma
 
Fecha de Ingreso: noviembre-2007
Mensajes: 4
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?
spike886 está desconectado   Responder Citando
Antiguo 21-nov-2007, 07:34   #2 (permalink)
r0d
r0d ha deshabilitado el karma
 
Fecha de Ingreso: noviembre-2007
Mensajes: 5
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
r0d está desconectado   Responder Citando
Antiguo 21-nov-2007, 15:36   #3 (permalink)
spike886 ha deshabilitado el karma
 
Fecha de Ingreso: noviembre-2007
Mensajes: 4
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
spike886 está desconectado   Responder Citando
Respuesta
No hay votos aún.


Herramientas
Desplegado

Normas de Publicación
No puedes crear nuevos temas
No puedes responder temas
No puedes subir archivos adjuntos
No puedes editar tus mensajes

BB code is Activado
Caritas están Activado
[IMG] está Activado
Código HTML está Desactivado


La Zona horaria es GMT -6. Ahora son las 20:01.


Message Board Statistics

LinkBacks Enabled by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93