简体   繁体   中英

Using c++11 unordered_map types on mac os x

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?

1) unordered_map is a form of map containter, use unordered_map::find(KeyType key) instead, which returns unordered_map::iterator , essentially pointer to std::pair< KeyType, ValueType > .

Or you can use unordered_map::operator[](KeyType key) to get access to element pointed by the key variable. Note that it will create new element in case it doesn't exist yet.

See cppreference.com for more info.

2) Use newer GCC (at least 4.7) with -std=c++11 option, and you will get standardized std::unordered_map instead of tr1::unordered_map

Your gcc version is too low. Besides, maybe you should install clang instead since Apple is the force behind it.

When you have upgraded to the latest version, you should not use tr1 anymore. tr1 is the draft for C++11 and is deprecated now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM