tabelle hash e vettori 2d
-
15-10-2019 - |
Domanda
Voglio spingere un vettore 2D in una riga della tabella hash per riga e poi la ricerca di una riga (vettore) nella tabella hash e vogliono essere in grado di trovarlo. Voglio fare qualcosa di simile
#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;
}
Non sono sicuro di come inserire e cercare un vettore in un set. Quindi, se nybody mi potesse guidare, sarà utile. Grazie
Aggiornamento:
std::set
come ho capito che non è una tabella hash ho deciso di utilizzare unordered_map
ma come devo fare per l'inserimento e la ricerca di elementi in questo:
#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;
}
Soluzione
Per inserire l'uso std::set::insert
, ala
myset.insert(v.begin(), v.end());
per trovare, utilizzare std::set::find
ala
std::set < vector<int> >::iterator it = myset.find(v[1]);
Esempio di funzionamento:
#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;
}
Altri suggerimenti
Per usare std::copy
per inserire in un set:
#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));
È inoltre possibile utilizzare assegnare, inserto del std::vector
, o copiare i costruttori a fare lo stesso.
Si utilizza una std::set
in questo esempio. Un set non ha un metodo di ricerca. È sufficiente scorrere l'insieme a fare operazioni su ogni elemento. Se si desidera cercare elementi specifici utilizzando un / tasto cancelletto, ti consigliamo di guardare strutture di dati come 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 ?
}
Non è corretto perché si sta cercando di copiare i numeri interi da ogni vettore in vettore di vettori nel set. Il vostro intento, e il tipo di set, indicano che si desidera che i 5 vettori da inserire nel vostro set. Si potrebbe quindi semplicemente fare questo (non per ciclo):
std::copy(v.begin(), v.end(), std::inserter(myset));