C++中unordered_map出现的若干问题详解(找不到库,找不到unorded_map,编译问题)

首先看一个例子:

<span style="font-size:18px;">#include<unordered_map></span>
<span style="font-size:18px;">#include<vector>
#include<string>

using namespace std;
int main(void) {
  unordered_map<int, vector<string> > test;
  return 1;
}</span>

如果采用g++进行编译的话,会出现不识别unordered_map的情况

输入g++ test1.cc -o test1进行编译

结果如下所示

<span style="font-size:18px;">test1.cc:8: error: ‘unordered_map’ was not declared in this scope</span>
<span style="font-size:18px;">test1.cc:8: error: expected primary-expression before ‘,’ token
test1.cc:8: error: expected primary-expression before ‘>’ token
test1.cc:8: error: ‘test’ was not declared in this scope
</span>

此时有两种方法来解决以上问题

方法1:

<span style="font-size:18px;">#include<tr1/unordered_map>//在unordered_map之前加上tr1库名,这是c++11之前的用法
#include<vector></span>
<span style="font-size:18px;">#include<string>

using namespace std;
using namespace std::tr1;//与此同时需要加上命名空间
int main(void) {
  unordered_map<int, vector<string> > test;
  return 1;
}</span>
此时,可顺利编译.

方法2:

此时代码仍然为

//#include<tr1/unordered_map>
#include<unordered_map>
#include<string>
#include<vector>
using namespace std;
//using namespace std::tr1;
int main(void) {
  unordered_map<int, vector<string> > test;
  return 1;
}

通过采用最新的编译器g++44进行编译,并进行相关的配置 

具体编译命令如下:

g++44 -std=c++0x test1.cc -o test1
其中g++44 是支持c++11的新特性。而-std=c++0x是ISOc++ 标准,这项支持现在仍处于试验阶段,所以必须添加该选项(若不添加还不好使, error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options. C++一点不惯你臭毛病)。


对这两种方法总结

首先,方法1中g++编译方法在这边采用了4.1版本的编译器不支持unordered_map的相关内容,如果想要让它支持,那么就得加tr1(g++从g++98开始支持tr1/unordered_map)。

第二种方法直接采用新的编译器g++44,完全彻底的解决问题。


最后,如果是在大型项目中交叉使用 第一种方法和第二种方法会编译不通过,此时需要观察每个Makefile,将其统一,建议全部统一为支持c++11新特性的方法。


这是到MLS的第一篇博客,在此表示对玥哥、博文兄、开练哥无私的帮助,希望能快点成长起来,为大家分忧解难,一起奋斗!

  • 9
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值