hash_set 在g++下编译出现not declare的问题
来源:互联网 发布:软件开发师培训 编辑:程序博客网 时间:2024/05/21 22:43
今天在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:这个问题不是什么大问题,偶然遇到了,就在此记录一下吧。
1 0
- hash_set 在g++下编译出现not declare的问题
- 在ubuntu下出现declare:not found的问题
- 在ubuntu下出现declare:not found的问题
- hash_set, hash_map在linux下编译出现not declared的解决方法
- 在Eclipse下编译C++程序出现的Launch failed.Binary not found.问题的解决方法
- Linux包含hash_map和hash_set的not declared问题
- 奇怪的NVL2问题,提示NOT DECLARE
- 在Solaris下编译socket出现的问题
- 在carbite c++下项目编译出现的问题
- 关于在linux下编译内核出现的一些问题
- 在vs2012下编译出现Msvcp120d.dll 丢失的问题
- 在linux(ubuntu)下编译VLC中出现的问题
- 【caffe-windows】在windows下编译caffe出现的问题
- Linux下g++编译C++连接oracle(OCCI)出现的问题及解决方式
- Ubuntu 12.04下用g++编译cplex实例程序,配置、出现的问题,解决办法
- Linux下g++编译C++连接oracle(OCCI)出现的问题及解决方式
- Linux下g++编译C++连接oracle(OCCI)出现的问题及解决方式
- Ubuntu 下运行 Shell 脚本时显示 "declare not found" 的问题
- 开始ruby旅程,开心快乐的学习
- 【转】s3c2440之外部中断
- CentOS使用本地光盘做yum源
- C++与C的那些差异
- 基于遗传算法的新安江模型参数优化率定(二)
- hash_set 在g++下编译出现not declare的问题
- Linux上ClearSilver的使用入门
- 基于遗传算法的新安江模型参数优化率定(三)
- php curl 模拟登陆https
- clearsilver 使用
- 基于遗传算法的新安江模型参数优化率定(四)
- request 对象
- 【读书笔记-数据挖掘概念与技术】数据立方体技术
- leetcode: Copy List with Random Pointer