Version compatibility issues with UNORDERED_MAP in C + + __c++

Source: Internet
Author: User
background

One of the most painful things in C + + is the unordered_map long-awaited, which is the only map in the earlier version of C + + standard library. But the internal implementation of the map is the use of red-black trees, as we all know, for dictionaries such structures can also be implemented with a hash table, that is, C + + standard library should also have hash_map this data structure. The red-black tree implements a map that takes up less memory, but looks inefficient, O (LOGN) lookup efficiency. The hash table implementation of the map occupies a large memory, but the search efficiency is high, often can approximate O (1) The amazing search efficiency.

The implementation of the hash table method for map in C + + is unordered_map this data structure, the first appearance should be in c++98 that era tr1 this namespace inside. Use the method to write more verbose.

#include <tr1/unordered_map>
using Std::tr1::unordered_map;

I think the data structure like UNORDERED_MAP is almost the daily necessities of C + + programmers. But because of the laziness of the C + + Standards Committee, it was dragged to the C++0X/C++11 standard to incorporate UNORDERED_MAP into STD standards. That is, only in the compiler that supports-std=c++0x and even-std=c++11 (for g++ It is probably g++4.4 this version to start supporting), you can use the following code unordered_map:

#include <unordered_map>
using Std::unordered_map;

Assuming that if all the world's g++ are already more than 4.4 version, there will be no problem with unordered_map, but the reality is brutal, always in some parts of the world server, still using g++-4.1.x, and for these servers to upgrade g++ is also very troublesome. All of our programs should consider compatibility with low-level versions. Solving Method

#if (__cplusplus = 201103L)
#include <unordered_map>
#include <unordered_set>
#else
# Include <tr1/unordered_map>
#include <tr1/unordered_set>
namespace std
{
    using std:: Tr1::unordered_map;
    Using Std::tr1::unordered_set;
}
#endif

The solution is to rely on __cplusplus the value of this macro in different C + + versions. For a compilation process that uses the-std=c++0x or-std=c++11 compilation options, the __cplusplus value is 201103L, otherwise it is a different value. Conclusions

The above method lowest probably can only support to c++98 this standard, because to c++98 only then began to have the TR1/UNORDERED_MAP, therefore your compiler if even c++98 all does not support, then obediently uses the ancient times the map which spreads to now. Reference http://www.cplusplus.com/forum/general/90675/http://stackoverflow.com/questions/2324658/ How-to-determine-the-version-of-the-c-standard-used-by-the-compiler

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.