哈希表和二维向量
我想将二维向量逐行推入哈希表,然后在哈希表中搜索行(向量)并希望能够找到它。我想做一些事情,比如
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于插入,请使用
std::set::insert
,ala对于查找,请使用
std::set::find
ala工作示例:
For inserting use
std::set::insert
, alafor find, use
std::set::find
alaWorking example:
使用
std::copy
插入集合:您还可以使用
std::vector
的赋值、插入或复制构造函数来执行相同的操作。在此示例中您使用的是
std::set
。集合没有查找方法。您只需遍历集合对每个项目进行操作即可。如果您想使用哈希/键查找特定项目,您将需要查看诸如std::map
之类的数据结构。To use
std::copy
to insert into a set: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 likestd::map
.这是不正确的,因为您试图将向量向量中每个向量的整数复制到集合中。您的意图以及集合的类型表明您希望将 5 个向量插入到您的集合中。然后你只需这样做(没有 for 循环):
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):