[英]Inserting typedef map into a hash table
在下面的程序中,我有一個typedef map
。 我想做的是實現一個 hash 表。 我正在嘗試使用unordered_map
,因為我聽說這是有效的,因為它需要 O(1) 時間。 我在我的主程序(我正在處理的另一個程序)中的任何地方都使用我的typedef map
,所以我不想改變它。 我想在其中一個函數中實現 hash 表,我試圖弄清楚如何將我的 map 的內容插入 hash 表並稍后搜索密鑰。 我在兩個遇到問題的地方插入了評論。 請幫忙。
#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 的評論后,我明白為什么我不能在unordered_map
中使用std::set
作為鍵,所以我嘗試使用std::string
作為鍵,但find
function 不起作用。 請你幫助我好嗎。
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;
完成這項工作所缺少的基本元素是為您用作密鑰的std::set
定義散列 function 。 STL 已經為std::set
定義了相等性和字典順序,因此您可以將其用作std::map
中的鍵值,而不會出現任何問題。 但是它沒有定義 hash function,所以這是你必須通過重載 std::hash 來做的事情。 這是相當簡單的,可以通過定義以下 function 來完成:
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
}
};
}
上面的函子 object 將返回size_t
的整數類型,並將std::set
作為參數。 您必須在namespace std
內定義它,以便std::unordered_map
能夠識別它。 由於您有一組類型int
,因此“簡單”算法可以簡單地將元素相加。 有更復雜的算法可以減少沖突的數量,這樣一個簡單的算法會以散列時間為代價而產生。 但是,一旦定義了這個,將std::set
鍵值插入unordered_map
以及創建新鍵值並在 hash 表中查找它們應該不會有任何問題。
您可以在以下位置查看源代碼示例: http://ideone.com/DZ5jm
編輯:傑森的代碼放在這里供參考:
#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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.