Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use unordered_map in Android?

I am trying to use a hash_map, defined in the Android NDK, but I get a "deprecation warning":

ndk/sources/cxx-stl/gnu-libstdc++/4.6/include/ext/../backward/backward_warning.h:33:2:
error: #warning This file includes at least one deprecated or antiquated header which may 
be removed without further notice at a future date. Please use a non-deprecated interface 
with equivalent functionality instead. For a listing of replacement headers and 
interfaces, consult the file backward_warning.h. To disable this warning use -Wno-
deprecated. [-Werror=cpp]

And since "unordered_map" is present in gnu-libstdc++/4.6/include/ and also in gnu-libstdc++/4.6/include/tr1/, I believe that there is a way to use it.

The point is that I cannot find it. Which of the following is the right one (if any):

#include <tr1/unordered_map.h>

#include <unordered_map>

And then, how to use it? __gnu_cxx::unordered_map is not recognized... and I don't know how to find this information.

like image 807
JonasVautherin Avatar asked Mar 21 '13 10:03

JonasVautherin


People also ask

What is the use of unordered_map?

unordered_map is an associated container that stores elements formed by the combination of a key value and a mapped value. The key value is used to uniquely identify the element and the mapped value is the content associated with the key. Both key and value can be of any type predefined or user-defined.

How is unordered_map implemented?

The std::unordered_map in C++ standard library is a really powerful data structure offering insertion, deletion, and lookup in O(1) amortized time. std::unordered_map is implemented as a hashmap or hashtable which are the same thing.

Is unordered map better than map?

When it comes to efficiency, there is a huge difference between maps and unordered maps. We must know the internal working of both to decide which one is to be used. You need ordered data. You would have to print/access the data (in sorted order).

How does unordered_map find work?

The C++ function std::unordered_map::find() finds an element associated with key k. If operation succeeds then methods returns iterator pointing to the element otherwise it returns an iterator pointing the map::end().


2 Answers

In case you don't want/need C++11 support, you can use the one from the STLPort using:

// Here we are referencing the stlport one:
#include <unordered_map>
...
std::tr1::unordered_map<int, int> test;

That's because STLPort defines unordered_map inside tr1 namespace, but the STLPort header is not inside any /tr1/ folder.

like image 193
Javier Sánchez LdG Avatar answered Oct 18 '22 02:10

Javier Sánchez LdG


I eventually found a way by adding C++11 support in my Android project. Pretty easy when we know it, but I took some time to figure it out. Neither STLPort nor Boost were needed. Once C++11 was integrated, I could use "unordered_map" as follows:

#include <unordered_map>
...
std::unordered_map<int, int> test;

I created a new question to explain how to enable C++11 support in Android here.

like image 23
JonasVautherin Avatar answered Oct 18 '22 02:10

JonasVautherin