Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将typedef映射插入哈希表_C++_Map_Set_Hashtable_Unordered Map - Fatal编程技术网

C++ 将typedef映射插入哈希表

C++ 将typedef映射插入哈希表,c++,map,set,hashtable,unordered-map,C++,Map,Set,Hashtable,Unordered Map,在下面的程序中,我有一个typedef映射。我想做的是实现一个哈希表。我尝试使用无序映射,因为我听说这是最有效的,因为它需要O(1)个时间。我在我的主程序(我正在处理的另一个程序)的任何地方都使用我的typedef映射,所以我不想改变它。我想在其中一个函数中实现哈希表,我正在尝试找出如何将映射的内容插入哈希表,并在以后搜索键。我在两个有问题的地方插入了一条评论。请帮忙 #include <iostream> #include <vector> #include <i

在下面的程序中,我有一个
typedef映射
。我想做的是实现一个哈希表。我尝试使用无序映射,因为我听说这是最有效的,因为它需要O(1)个时间。我在我的主程序(我正在处理的另一个程序)的任何地方都使用我的
typedef映射
,所以我不想改变它。我想在其中一个函数中实现哈希表,我正在尝试找出如何将映射的内容插入哈希表,并在以后搜索键。我在两个有问题的地方插入了一条评论。请帮忙

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

要实现这一点,您缺少的基本元素是为您要用作键的
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
        }
    };
}

我想我说错了,说是订的。它不是,我会编辑它。但是我仍然可以使用
sample.find(s)
映射中查找值,其中
s
是程序中定义的
std::set
。我可以对无序映射执行同样的操作吗?请注意:您可以在一组严格(弱)排序的元素上定义严格(弱)排序:
set1
if
*set1.begin()<*set2.begin()
。如果第一个(即最小)元素相等,则可以迭代。如果所有元素都相等,则长度相等的容器相等,否则元素最少的容器较小。这是适用于数字的规范顺序(从右到左)。您也可以这样做,但同样,如果您将键值存储为指向集合的指针,则不必担心键值的上述相等规则。。。指向两个不同集合的指针不会相等(不能相等),这一事实为区分集合提供了一种很好的方法。此外,指针易于散列和排序,因此它们对于
std::map
std::unordered_map
都是很好的键值。为了防止内存泄漏,您可以将它们存储在
std::shared\u ptr
@bitmask。。。是的,非常正确。。。对字符串进行词汇排序也可以做到这一点。我不是想说这不可能,而是说这太麻烦了。。。任何快速的技术都是行不通的。定义词汇顺序可能是一个耗时的过程,使用指针可以完全避免这个问题。如果您能给出一个示例或编辑我的代码,这将有助于我更好地理解。非常感谢。请确保您使用C++0x支持进行编译。。。这可能需要您使用gcc设置适当的标志,例如
gcc-std=c++0x
gcc-std=gnu++0x
。。。您还需要重新定义键值,因为如果不使用辅助函数,就无法将
int
直接转换为
string
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
        }
    };
}
#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;
}