質問

2Dベクトルを列ごとにハッシュテーブルの列に押し込み、後でハッシュテーブルで行(ベクトル)を検索し、見つけたいと思っています。ようなことをしたいです

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

セット内のベクトルを挿入して検索する方法がわかりません。したがって、NYBodyが私を導くことができれば、それは役に立ちます。ありがとう

アップデート:

私が気づいたように std::set 私が使用することにしたハッシュテーブルではありません unordered_map しかし、どうすればこれに要素を挿入して見つける必要がありますか:

#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;
}
役に立ちましたか?

解決

使用を挿入するため std::set::insert, 、アラ

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

検索するには、使用してください std::set::find アラ

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

作業例:

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

他のヒント

使用する std::copy セットに挿入するには:

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

使用することもできます std::vectorコンストラクターを割り当て、挿入、またはコピーして、同じことを行います。

あなたはaを使用しています std::set この例では。セットにはルックアップ方法がありません。各アイテムの操作を行うセットを繰り返します。ハッシュ/キーを使用して特定のアイテムを調べたい場合は、次のようなデータ構造を確認する必要があります。 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 ?
}

ベクトルのベクトルの各ベクトルから整数をセットにコピーしようとしているため、それは正しくありません。あなたの意図とセットのタイプは、5つのベクトルをセットに挿入したいことを示します。それからあなたは単にこれを行うでしょう(ループの場合はありません):

std::copy(v.begin(), v.end(), std::inserter(myset));
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top