Ver Mensaje Individual
  #4 (permalink)  
Antiguo 24/09/2009, 01:39
Avatar de r0d
r0d
 
Fecha de Ingreso: noviembre-2007
Mensajes: 86
Antigüedad: 16 años, 5 meses
Puntos: 3
Respuesta: vectores 2D c++

sino, en la libreria boost hay una clase para hacer vectores de 'n' dimensiones: boost::multi_array

o sino, te propongo la clase que suelo usar para un vector de 2 dimensiones (cuidad, no es thread-safe):

Código:
#ifndef __H_DYN_2D_MATRIX_H__
#define __H_DYN_2D_MATRIX_H__

// STL
#include <vector>
#include <algorithm>

// RLib
#include "Tools.h"


namespace RLib
{

/**
*	A basic classic template matrix class
* 	/!\ Warning /!\  The access of the elements of the matrix ARE NOT safes y DO NOT generate exceptions
*	Thus you must take care of every access you made to the elements of the matrix.
*/
template <typename T>
class Dyn2dMatrix
{
public:
	//! contructor
	Dyn2dMatrix(const size_t nbRow = 0, const size_t nbCol = 0)
		: m_nbRow(nbRow)
		, m_nbCol(nbCol)
		, m_array(nbRow*nbCol)
	{}

	//! copy constructor
	Dyn2dMatrix( const Dyn2dMatrix<T> & matrix )
		: m_nbRow( matrix.m_nbRow )
		, m_nbCol( matrix.m_nbCol )
		, m_array( std::vector<T>( matrix.Size() ) )
	{
		std::copy( matrix.m_array.begin(), matrix.m_array.end(), m_array.begin() );
	}

	//! assignment operator
	void operator = ( const Dyn2dMatrix & matrix )
	{
		m_nbRow = matrix.m_nbRow;
		m_nbCol = matrix.m_nbCol;
		m_array = std::vector<T>( matrix.Size() );
		std::copy( matrix.m_array.begin(), matrix.m_array.end(), m_array.begin() );
	}

	//! resize the matrix and refill all with the given value
	void Resize(const size_t nbRow, const size_t nbCol, const T& value)
	{
		m_nbRow = nbRow;
		m_nbCol = nbCol;
		//std::vector<T>(m_nbRow * m_nbCol).swap( m_array );
		//Fill( value );
		m_array.resize( m_nbRow * m_nbCol, value ); // before a set of tests, it appear that a resize is faster than swap
	}

	//! FromString
	bool FromString( const std::string & strIn )
	{
		StringVector strVect = Tools::Explode(strIn, ' ');
		if ( strVect.size() != m_array.size() ) return false;
		size_t count = 0;
		for ( StringVectorIt it = strVect.begin(); it != strVect.end(); it++ )
		{
			m_array[count++] = Tools::FromString<T>( *it );
		}
		return true;
	}

	//! destructor
	~Dyn2dMatrix()
	{
		m_array.clear();
	}

	//! accessor
	T operator () ( const size_t col, const size_t row ) const
	{
		return m_array[m_nbCol*row+col];
	}

	//! get matrix width ( nb col )
	size_t Width() const { return m_nbCol; }

	//! get matrix height ( nb row )
	size_t Height() const { return m_nbRow; }

	//! total number of elements
	size_t Size() const { return m_array.size(); }

	//! mutator
	T & operator () ( const size_t col, const size_t row )
	{
		return m_array[m_nbCol*row+col];
	}

	//! mutator
	T & operator () ( const size_t pos )
	{
		return m_array[pos];
	}

	//! fill the matrix with the same value
	void Fill( const T & value )
	{
		std::fill( m_array.begin(), m_array.end(), value);
	}

	//! replace element
	void Replace( const T & before, const T & after )
	{
		for ( size_t i = 0; i<m_array.size(); i++ )
			if ( m_array[i] == before )
				m_array[i] = after;
	}

	//! ToString
	std::string ToString() const
	{
		std::string strRet;
		for ( size_t i = 0; i<m_array.size(); i++ )
		{
			strRet += Tools::ToString<T>( m_array[i] );
			if ( ( (i+1) % m_nbCol) == 0 )
				strRet += "\n";
		}
		return strRet;
	}

private:
	//! matrix size
	size_t m_nbRow, m_nbCol;

	//! datas
	std::vector<T> m_array;
};

}; // end namepace RLib

#endif // __H_DYN_2D_MATRIX_H__