在下面的程序中,我有一个 typedef 映射
。我想做的是实现一个哈希表。我正在尝试使用 unordered_map
因为我听说这是高效的,因为它需要 O(1) 时间。我在主程序(我正在处理的另一个程序)中到处都使用 typedef map
,所以我不想更改它。我想在其中一个函数中实现哈希表,我试图弄清楚如何将我的 map 的内容插入哈希表并稍后搜索 key 。我在两个遇到问题的地方插入了评论。请帮忙。
#include <iostream>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <unordered_map>
using namespace std;
typedef vector<int> v_t;
typedef set<int> s_t;
typedef map<s_t, v_t> m_t;
typedef m_t::iterator m_it;
typedef std::unordered_map<s_t, v_t> Mymap;
int main(){
m_t sample;
for (int i = 0; i < 100; i = i+2) {
v_t v;
for(int k = 100 ; k<=105 ; ++k)
v.push_back(k);
s_t k;
k.insert(i);
sample.insert(sample.end(), make_pair(k, v));
}
//---------Debug--------------------
for( m_it it(sample.begin()) ; it!=sample.end(); ++it) {
cout << "Key: ";
copy(it->first.begin(), it->first.end(), ostream_iterator<int>(cout, " "));
cout << " => Value: ";
copy (it->second.begin(),it->second.end(),ostream_iterator<double>(cout," "));
cout << endl;
}
//---------------------------------
Mymap c1;
for( m_it it(sample.begin()) ; it!=sample.end(); ++it) {
c1.insert(Mymap::value_type(it->first,it->second)); // how to do this ?
}
s_t s;
s.insert(72);
if(c1.find(s)!=c1.end()) // does this work ?
cout << "Success" << endl;
return 0;
}
感谢任何帮助或评论。
阅读 Jason 的评论后,我明白了为什么我不能使用 std::set
作为 unordered_map
中的键,所以我尝试使用 std::string
作为键,但 find
功能将不起作用。请你帮助我好吗。
Mymap c1;
for( m_it it(sample.begin()) ; it!=sample.end(); ++it) {
v_t v1;
std::string key;
key.insert(key.begin(),it->first.begin(),it->first.end());
copy(it->second.begin(), it->second.end(),std::back_inserter(v1));
c1.insert(Mymap::value_type(std::make_pair(key,v1)));
}
string s = "72";
if((c1.find(s) != c1.end()) == true)
cout << "Success" << endl;
return 0;
最佳答案
要完成这项工作,您缺少的基本要素是为用作 key 的 std::set
定义一个散列函数。 STL 已经为 std::set
定义了相等性和字典顺序,因此您可以将它用作 std::map
中的键值,而无需任何问题。但是它没有定义散列函数,所以这是你必须通过重载 std::hash 来做的事情。这非常简单,可以通过定义以下函数来完成:
namespace std
{
template<>
struct hash<std::set<int> > : public std::unary_function<std::set<int>, size_t>
{
size_t operator()(const std::set<int>& my_set) const
{
//insert hash algorithm that returns integral type
}
};
}
上述仿函数对象将返回 size_t
的整数类型,并将 std::set
作为参数。您必须在 namespace std
中定义它,以便 std::unordered_map
能够识别它。一个“简单”的算法可以简单地对元素求和,因为您有一组 int
类型。有更复杂的算法可以减少这种简单算法以散列时间为代价产生的冲突次数。一旦你定义了这个,你应该不会有任何问题将你的 std::set
键值插入到 unordered_map
中,以及创建新的键值和在哈希表中找到它们。
您可以在以下位置查看您的源代码示例:http://ideone.com/DZ5jm
编辑:杰森的 code放在这里供引用:
#include <iostream>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <unordered_map>
using namespace std;
namespace std
{
template<>
struct hash<set<int> > : public unary_function<set<int>, size_t>
{
size_t operator()(const std::set<int>& my_set) const
{
set<int>::iterator iter = my_set.begin();
int total = 0;
for (; iter != my_set.end(); iter++)
{
total += *iter;
}
return total;
}
};
}
typedef vector<int> v_t;
typedef set<int> s_t;
typedef map<s_t, v_t> m_t;
typedef m_t::iterator m_it;
typedef std::unordered_map<s_t, v_t> Mymap;
int main(){
m_t sample;
for (int i = 0; i < 100; i = i+2) {
v_t v;
for(int k = 100 ; k<=105 ; ++k)
v.push_back(k);
s_t k;
k.insert(i);
sample.insert(sample.end(), make_pair(k, v));
}
//---------Debug--------------------
for( m_it it(sample.begin()) ; it!=sample.end(); ++it) {
cout << "Key: ";
copy(it->first.begin(), it->first.end(), ostream_iterator<int>(cout, " "));
cout << " => Value: ";
copy (it->second.begin(),it->second.end(),ostream_iterator<double>(cout," "));
cout << endl;
}
//---------------------------------
Mymap c1;
for( m_it it(sample.begin()) ; it!=sample.end(); ++it) {
c1.insert(Mymap::value_type(it->first,it->second)); // how to do this ?
}
s_t s;
s.insert(72);
if(c1.find(s)!=c1.end()) // does this work ?
cout << "Success" << endl;
return 0;
}
关于c++ - 将 typedef 映射插入哈希表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6632064/