2

I'm on MAC OS X 10.8.4 and have installed gcc by downloading XCode and the command line tools package. Here is my gcc:

Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)

I'm trying to use the new unordered_map from the tr1 namespace:

using namespace std;
#include <tr1/unordered_map>

template<class T>
class A 
{
    tr1::unordered_map<T, int> * mymap;
    .....
    // key is of type T and value is of type int
    mymap->at(key) = value;
}

However, the line where I'm accessing the map element using at is not compiling. I'm getting the following error:

error: 'class std::tr1::unordered_map<int, int, std::tr1::hash<int>, std::equal_to<int>, std::allocator<std::pair<const int, int> >, false>' has no member named 'at'

I don't quite understand this since the C++ reference does include a function called at to access individual elements of the map. I tried to look into the header files containing the map declaration (on my system it is at /usr/include/c++/4.2.1/tr1/unordered_map) and surprisingly enough, it does not contain a member named at.

I understand that older compilers might not be supporting new c++11 libraries in the std namespace, but I've been able to use some of the other new libraries from tr1 namespace. In fact, I can use other functions of unordered_map like find, insert etc. Only the member at is missing.

How can I get this compiled? Should I upgrade to a newer compiler? I don't see the new compilers available for mac os x readily. Should I build it from scratch?

4

2 回答 2

4

1)unordered_map是容器的一种形式map,改为使用unordered_map::find(KeyType key),它返回unordered_map::iterator,本质上是指向 的指针std::pair< KeyType, ValueType >

或者您可以使用来访问变量unordered_map::operator[](KeyType key)指向的元素。key请注意,如果它尚不存在,它将创建新元素。

有关详细信息,请参阅cppreference.com

2)使用较新的GCC(至少4.7)-std=c++11选项,你会得到标准化std::unordered_map而不是tr1::unordered_map

于 2013-08-31T13:58:47.263 回答
4

你的 gcc 版本太低了。此外,也许你应该安装 clang,因为 Apple 是它背后的力量。

当您升级到最新版本后,您不应该再使用tr1了。tr1是 C++11 的草案,现已弃用。

于 2013-08-31T13:59:36.710 回答