开发者

hash tables and 2d vectors

开发者 https://www.devze.com 2023-02-02 04:11 出处:网络
I want to push a 2d vector into a hash table row by row and later search for a row (vector) in the hash table and want to be able to find it. I want to do something like

I want to push a 2d vector into a hash table row by row and later search for a row (vector) in the hash table and want to be able to find it. I want to do something like

#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)开发者_Go百科); // 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;
}

I'm not sure how to insert and look up a vector in a set. So if nybody could guide me, it will be helpful. Thanks

update:

as i realized std::set is not an hash table I decided to use unordered_map but how should I go about inserting and finding elements in this:

#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;
}


For inserting use std::set::insert, ala

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

for find, use std::set::find ala

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

Working example:

#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;
}


To use std::copy to insert into a 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));

You can also use std::vector's assign, insert, or copy constructors to do the same.

You are using a std::set in this example. A set does not have a lookup method. You simply iterate through the set doing operations on each item. If you want to look up specific items using a hash/key, you'll want to look at data structures like 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 ?
}

It's not correct because you're trying to copy integers from each vector in your vector of vectors into the set. Your intent, and the type of your set, indicate you want the 5 vectors to be inserted into your set. You would then simply do this (no for loop):

std::copy(v.begin(), v.end(), std::inserter(myset));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号