使用unordered_map编译报#error This file requires compiler and library support

来源:互联网 发布:羽毛球鹰眼算法 编辑:程序博客网 时间:2024/06/05 03:18

最近常用到unordered_map,但是我发现这个库在vc6中不能用,无赖只好换成vs2012,测试可以用。然而,我在linux下用g++编译时,出现了以下错误:

In file included from /usr/include/c++/4.8/unordered_map:35:0,
                 from cul.cc:3:
/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support for the \
  ^
cul.cc: In member function ‘int Solution::lengthOfLongestSubstring(std::string)’:
cul.cc:15:4: error: ‘unordered_map’ was not declared in this scope
    unordered_map<char,int> hash;

....
    

后面的信息可以看出,是说unordered_map没有定义,我在网上查找了下发现g++4.4之后都支持,我用的是4.8,应该可以正常使用的。最后看到这句:This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.发现时需要编译时加上-std=c++11这个参数,才可以使用,如下:

g++ -std=c++11 -g -o cu cul.cc

发现编译OK!!

0 0