【转】Using hash_map on GCC

If you have tried to use some STL containers with GCC, such as hash_map:
// error: hash_map: No such file or directory
#include <hash_map>

int main()
{

// error: ‘hash_map’ is not a member of ‘std’
std::hash_map<int,int> hm;

return 0;
}

Then you have realized that the code above does not compile. That's because on GCC, hash_map is not regarded as a standard container, but rather as a extension included in the __gnu_cxx namespace. In order to use hash_map and other extended containers with a minimum impact in your code (which is very important if it's intended to be cross-platform), you can use the following solution:

#ifdef __GNUC__
#include <ext/hash_map>
#else
#include <hash_map>
#endif


namespace std
{
using namespace __gnu_cxx;
}

int main()
{

std::hash_map<int,int> hm;

return 0;
}

Hope that helps.

 

#include <iostream>
#include <string>

#ifdef __GNUC__
#include <ext/hash_map>
#else
#include <hash_map>
#endif


namespace std
{
using namespace __gnu_cxx;
}

int main()
{
using namespace std;

hash_map<int, string> mapStudent;

mapStudent.insert(hash_map<int, string>::value_type (1, "student_one"));

mapStudent.insert(hash_map<int, string>::value_type (2, "student_two"));

mapStudent.insert(hash_map<int, string>::value_type (3, "student_three"));

hash_map<int, string>::iterator iter;

for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)

{

cout<<iter->first<<" "<<iter->second<<endl;

}
return 0;
}

#include ; #include ; using namespace std; using namespace __gnu_cxx; //是处在这个名空间内,源文件最后一行 int main() { crope str1("Hello"); crope str2("world"); crope str3=str1+str2; cout< <
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值