哈希表和二维向量

发布于 2024-10-10 23:49:10 字数 1580 浏览 1 评论 0原文

我想将二维向量逐行推入哈希表,然后在哈希表中搜索行(向量)并希望能够找到它。我想做一些事情,比如

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

我不确定如何在集合中插入和查找向量。因此,如果有人可以指导我,那将会很有帮助。谢谢

更新:

当我意识到 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;
}

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)); // 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝眸 2024-10-17 23:49:10

对于插入,请使用 std::set::insert,ala

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

对于查找,请使用 std::set::find ala

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

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;
}
稍尽春風 2024-10-17 23:49:10

使用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的赋值、插入或复制构造函数来执行相同的操作。

在此示例中您使用的是 std::set。集合没有查找方法。您只需遍历集合对每个项目进行操作即可。如果您想使用哈希/键查找特定项目,您将需要查看诸如 std::map 之类的数据结构。

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.

看轻我的陪伴 2024-10-17 23:49:10
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 个向量插入到您的集合中。然后你只需这样做(没有 for 循环):

std::copy(v.begin(), v.end(), std::inserter(myset));
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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文