Foros del Web » Programación para mayores de 30 ;) » C/C++ »

vectores 2D c++

Estas en el tema de vectores 2D c++ en el foro de C/C++ en Foros del Web. Hola gente sigo con el tema de los vectores. Ahora me interesan los vectores de 2 dimensiones. Encontre un par de ejemplos en internet y ...
  #1 (permalink)  
Antiguo 28/07/2008, 06:54
 
Fecha de Ingreso: febrero-2008
Mensajes: 42
Antigüedad: 16 años, 2 meses
Puntos: 0
vectores 2D c++

Hola gente sigo con el tema de los vectores. Ahora me interesan los vectores de 2 dimensiones.

Encontre un par de ejemplos en internet y este codigo. Que carga el valor de K. A cada valor del vector. Y con un juego de For se puede cargar.


#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

int main()
{
vector<vector<int> > items;
int k = 0;

for ( int i = 0; i < 10; i++ ) {
items.push_back ( vector<int>() );

for ( int j = 0; j < 2; j++ )
items[k++].push_back ( k++ );
}

for ( int i = 0; i < 3; i++ ) {
for ( int j = 0; j < 2; j++ )
cout<< setw ( 3 ) << items[i][j] <<' ';
cout<<'\n';
}

cin.get();
return 0;
}

Ahora! lo que no entiendo es el proceso. Yo estoy acostumbrado a los arrays array [x][y]. Y no encuentro la forma de cargarlos correctamente... Osea lo que yo quiciera cargar es algo asi.

array[0][0] = algo;
array[0][1] = otro;

array[1][0] = algo;
array[1][1] = otro;

array[2][0] = algo;
array[2][1] = otro;

array[3][0] = algo;
array[3][1] = otro;

A alguien se le ocurre como puede ser? Gracias!
  #2 (permalink)  
Antiguo 28/07/2008, 17:59
 
Fecha de Ingreso: julio-2008
Mensajes: 83
Antigüedad: 15 años, 9 meses
Puntos: 6
Respuesta: vectores 2D c++

mivector.at(2).at(1) = algo;
mivector.at(2).at(2) = algo;

Saludos.
  #3 (permalink)  
Antiguo 23/09/2009, 23:51
 
Fecha de Ingreso: septiembre-2009
Mensajes: 1
Antigüedad: 14 años, 7 meses
Puntos: 0
Respuesta: vectores 2D c++

mejor asi
vector<double> Coordenadas(3);
Coordenadas[0]=12.5;
Coordenadas[1]=2.3;
Coordenadas[2]=3.5;

for (int i=0;i<Coordenadas.size() ;i++ )
cout<<"Coordenada : "<<(char)(i+88)<<"\t"<<Coordenadas[i]<<endl;
  #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__
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:32.