Pregunta

Quiero empujar un vector 2D en una fila de la tabla hash fila y después buscar una fila (vector) en la tabla hash y quieren ser capaces de encontrarlo. Quiero hacer algo como

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

int main(){

std::set < vector<int> > myset;

vector< vector<int> > v;

int k = 0;

for ( int i = 0; i < 5; i++ ) {
 v.push_back ( vector<int>() );

for ( int j = 0; j < 5; j++ )
 v[i].push_back ( k++ );
}

for ( int i = 0; i < 5; i++ ) {
  std::copy(v[i].begin(),v[i].end(),std::inserter(myset)); // This is not correct but what is the right way ?

// and also here, I want to search for a particular vector if it exists in the table. for ex. myset.find(v[2].begin(),v[2].end()); i.e if this vector exists in the hash table ?

}

  return 0;
}

No estoy seguro de cómo insertar y buscar un vector en un conjunto. Así que si nybody podía guiar, será útil. Gracias

Actualización:

std::set como me di cuenta de que no es una tabla hash que decidí usar unordered_map pero ¿cómo hago para la inserción y la búsqueda de elementos en esto:

#include <iostream>
#include <tr1/unordered_set>
#include <iterator>
#include <vector>
using namespace std;

typedef std::tr1::unordered_set < vector<int> > myset;

int main(){
myset c1;
vector< vector<int> > v;

int k = 0;

for ( int i = 0; i < 5; i++ ) {
 v.push_back ( vector<int>() );

for ( int j = 0; j < 5; j++ )
 v[i].push_back ( k++ );
}

for ( int i = 0; i < 5; i++ )  
 c1.insert(v[i].begin(),v[i].end()); // what is the right way? I want to insert vector by vector. Can I use back_inserter in some way to do this?

// how to find the vectors back?

  return 0;
}
¿Fue útil?

Solución

Para insertar uso std::set::insert, ala

myset.insert(v.begin(), v.end());

para encontrar, usar std::set::find ala

std::set < vector<int> >::iterator it = myset.find(v[1]);

Ejemplo de trabajo:

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

int main()
{
  typedef vector<int> int_v_t;
  typedef set<int_v_t> set_t;

  set_t myset;

  // this creates 5 items 
  typedef vector<int_v_t> vec_t;
  vec_t v(5);

  int k = 0;

  for(vec_t::iterator it(v.begin()), end(v.end()); it != end; ++it)
  {
   for (int j = 0; j < 5; j++)
    it->push_back(k++);
  }

  // this inserts an entry per vector into the set 
  myset.insert(v.begin(), v.end());

  // find a specific vector
  set_t::iterator it = myset.find(v[1]);

  if (it != myset.end()) cout << "found!" << endl; 

  return 0;
}

Otros consejos

Para utilizar std::copy para insertar en un conjunto:

#include <algorithm>
#include <iterator>
#include <vector>

std::vector<int> v1;
// Fill in v1 here
std::vector<int> v2;
std::copy(v1.begin(), v1.end(), std::back_inserter<std::vector<int> >(v2));

También puede utilizar Asignar, inserción de std::vector, o copiar constructores para hacer lo mismo.

Está utilizando un std::set en este ejemplo. Un conjunto no tiene un método de búsqueda. Sólo tiene que iterar a través del conjunto haciendo operaciones en cada artículo. Si desea buscar artículos específicos utilizando una clave / hachís, usted querrá ver las estructuras de datos como std::map.

for ( int i = 0; i < 5; i++ ) {
  std::copy(v[i].begin(),v[i].end(),std::inserter(myset)); // This is not correct but what is the right way ?
}

No es correcto porque usted está tratando de copiar números enteros de cada vector en su vector de vectores en el conjunto. Su intención, y el tipo de su conjunto, indican desea que los 5 vectores para insertar en su conjunto. A continuación, simplemente hacer esto (sin bucle):

std::copy(v.begin(), v.end(), std::inserter(myset));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top