hash_set 在g++下编译出现not declare的有关问题

hash_set 在g++下编译出现not declare的问题

今天在Ubuntu下写一个程序的时候用到了hash_set容器,出现一点问题,在这里记录一下,先说一下,hash_*系列例如hash_map,hash_set 等已经被废弃了,C++11用unordered_map,unordered_set等来替代,但是我用的g++ 4.6.1警告说C++11特性是实验性的,貌似到4.8才完全支持。所以就改用hash_set了,写了如下的程序:

#include <iostream>
#include <string>
#include <hash_set>

using namespace std;

int main()
{
    hash_set<string> ss;
    hash_set<string>::iterator it;
    string s="hello";
    ss.insert(s);
	s="anna";
	ss.insert(s);
	s="today";
	ss.insert(s);
	s="is";
	ss.insert(s);
	s="bad!";
	ss.insert(s);
	for(it=ss.begin();it!=ss.end();it++)
	{
		std::cout<<(*it)<<" ";
	}
	std::cout<<endl;
}



直接报错:error: 'hash_set' was not declared in this scope

奇怪了,明明是包含了头文件的,搜索才发现原来hash_set是在__gnu_cxx这个namespace里面的,加入

using namespace __gnu_cxx后编译,直接跳到hashtable.h的源码中hasher函数这里,出错显示:error: no match for call to '(const hasher {aka const __gnu_cxx::hash<std::basic_string<char> >}) (const key_type&)'

这个问题想了一下应该是hash函数不支持string类的原因,但是这个问题怎么解决,自己却不知道,又在网上找了半天,在网上找到一个解决办法,见如下代码:


#include <iostream>
#include <string>
#include <hash_set>

namespace __gnu_cxx {
    template<>
    struct hash<std::string>
    {
        hash<char*> h;
        size_t operator()(const std::string &s) const
        {
            return h(s.c_str());
        };
    };
}

using  namespace __gnu_cxx;
using namespace std;

int main()
{
    hash_set<string> ss;
    hash_set<string>::iterator it;
    	string s="hello";
	ss.insert(s);
	s="anna";
	ss.insert(s);
	s="today";
	ss.insert(s);
	s="is";
	ss.insert(s);
	s="bad!";
	ss.insert(s);
	for(it=ss.begin();it!=ss.end();it++)
	{
		std::cout<<(*it)<<" ";
	}
	std::cout<<endl;
}
终于可以运行成功,添加的一部分主要是让string类转化成char *指针后可以才能被hash吧。

PS:这个问题不是什么大问题,偶然遇到了,就在此记录一下吧。