64

Quick question...Is hash_map part of the STL?

razlebe
  • 7,134
  • 6
  • 42
  • 57
Jake
  • 2,877
  • 8
  • 43
  • 62
  • 2
    No. Use `unordered_map`. Do you really need a hash map? If not see if `std::map` meets your actual requirements. – Martin York May 06 '11 at 08:17
  • 6
    @Martin: `hash_map` ***is*** part of the STL. – Alexandre C. May 06 '11 at 08:21
  • 11
    Please define STL. Do you mean the C++ standard library? Or do you mean the original Standard Template Library, released many years ago that was later partially adopted as the current C++ standard? Normally we don't really care if you use STL informally as stdlib, but in this case it matters. Standard C++ does not have `hash_map`, STL does. (Future C++ and Boost have `unordered_map`, a hash map.) – GManNickG May 06 '11 at 08:28

6 Answers6

112

The STL has hash_map, but the C++ Standard Library does not.

Due to a common misconception, you may think of the C++ Standard Library as "the STL", or of parts of your toolchain's implementation of the C++ Standard Library as "an STL implementation".

It is not.

It is also a great shame that both MSVC++ and GCC (which implement hash_map as a compiler-specific extension), place it in the std namespace, which is not only highly misleading, but also illegal per the standard. *sigh*

C++11 has introduced std::unordered_map, which is not dissimilar.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
19

Quoting Wikipedia (emphasis added):

From the STL page:

The Standard Template Library (STL) is a software library partially included in the C++ Standard Library.

...and then from the hash_map page

In the C++ programming language, hash_map is the name of a hashed associative container in the Standard Template Library. It is provided by several implementors, such as the GNU C++ compiler and Microsoft's Visual C++. It is not part of the C++ Standard Library, but the C++ Technical Report 1 contains the very similar container unordered_map, which will be included in the upcoming C++0x standard.

So in short,

  • YES it's part of the STL.
  • But it IS NOT part of the standard library.
  • But it is supported by several very popular implementations.
razlebe
  • 7,134
  • 6
  • 42
  • 57
  • 3
    @razlebe: you couldn't just say yes or no? – forsvarir May 06 '11 at 08:13
  • 7
    @forsvarir: Of course, but quoting wikipedia lends my answer some credibility. ;) – razlebe May 06 '11 at 08:14
  • Importantly, knowing it's only moderately portable, if you choose to use it anyway then do check your compiler docs to find out which header and namespace it's in - it probably isn't a simple `#include ` / `std::hash_map x`, though it won't be much harder.... – Tony Delroy May 06 '11 at 08:14
  • 4
    @forsvarir: it's difficult here. OP's question must be answered by "yes", but OP's intended question should be answered by "no"... – Alexandre C. May 06 '11 at 08:27
  • @Alexandre C.: Sorry, mild work banter (I work with razelbe)... it can be difficult to get a straight answer out of him sometimes :) – forsvarir May 06 '11 at 08:32
  • Even "partially included" is misleading, as it implies a verbatim copy and an ongoing dynamic identicality, neither of which are accurate. – Lightness Races in Orbit May 06 '11 at 08:38
  • @Tomalak Geret'kal - Entirely woolly, I agree. But then so is the definition of the term STL, as demonstrated by the lack of concensus across the answerers here. – razlebe May 06 '11 at 08:40
  • @Tomalak Geret'kal: Thanks. :) – razlebe May 06 '11 at 08:41
  • @razlebe: There is [only _one_ definition](http://stackoverflow.com/questions/5205491/whats-this-stl-vs-c-standard-library-fight-all-about/5205571#5205571) of the term STL, even if most C++ programmers for whatever reason stubbornly refuse to accept it! :) – Lightness Races in Orbit May 06 '11 at 08:41
  • @Tomalak Geret'kal Who says? :-) Seriously, who has the authority to say. The meaning of a word is determined by a consensus among the users and listeners. As is apparent from the comments here, there is no consensus with regards to **STL**. – James Kanze May 06 '11 at 08:45
  • @JamesKanze: History, logic and me. ;) Most of the lack of consensus, frankly, is driven by ignorance (and I mean that with the greatest of respect) because several high-profile authors have unfortunately chosen an inaccurate term and so learning institutions have taught wrongly. Yes, I say "wrongly" because they are _causing_ the problem, not falling into the trap that it causes. – Lightness Races in Orbit May 06 '11 at 08:49
  • @Tomalak Geret'kal: I agree that ignorance may have been one of the root causes for the lack of consensus to begin with, but the current situation is that there is a lack of consensus, even among very intelligent and very competent people. So we have to take that into account if we want to communicate effectively. – James Kanze May 06 '11 at 08:59
  • @JamesKanze: It's possible to communicate effectively by always using the most logical, correct and accurate term, assuming the audience has not been blinded by the ignorance, and then bash them over the head with the truth if they look confused. Every time. It might be a bit irritating, but after a few hundred years only the correct term will be used. Let's start now! – Lightness Races in Orbit May 06 '11 at 09:01
8

The problem is that there is no agreed upon meaning for the term STL. Is hash_map part of Standard C++? No, it isn't. unordered_map will be part of the new C++ standard, however, and is a map implemented using hashing.

5

Yes, hash_map is part of the STL. However, it is not part of C++03's standard library.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Alexandre C.
  • 55,948
  • 11
  • 128
  • 197
  • Really? I couldn't find `hash_map` in the C++0x FDIS. Do you have any evidence to support your claim? – fredoverflow May 06 '11 at 08:25
  • 4
    @FredOverflow: Despite your misconception, there is _only one STL_. – Lightness Races in Orbit May 06 '11 at 08:32
  • @Tomalak Geret'kal: The meaning of words is determined by a consensus among the users. Regretfully, in the case of **STL**, there isn't really a consensus, and we have to consider that it has several different meanings, depending on who is using it. (The usage I prefer is that **STL** is the library defined at http://www.sgi.com/tech/stl/, in which case, it contains `hash_map`. But not everyone uses the word in this way, so unless you know the author, you have to ask what they mean by it. – James Kanze May 06 '11 at 08:42
  • @Tomalak: That's technically correct. Still, I think that Jake is not even aware of the STL and actually meant the C++ standard library. – fredoverflow May 06 '11 at 08:44
  • @FredOverflow: I agree. Yet another reason why the continued misuse of the term is dangerous and silly. – Lightness Races in Orbit May 06 '11 at 08:44
  • 1
    @JamesKanze: I have decided the meaning of the word based on its technical and historical accuracy, and you shall follow me according to the word of Tomalak. This jovial response brought to you by the fact that you nevertheless prefer the proper meaning. – Lightness Races in Orbit May 06 '11 at 08:45
  • 4
    @Tomalak You (nor any other single person) can decide on the meaning of a word; meanings are established by a consensus. If it is just you and I discussing things together, we have a consensus---I know a couple of other people who agree with it as well (e.g. Bjarne Stroustrup). In an open forum like this, we have to take into account the consensus of the entire community, and regretfully, in the case of **STL**, there isn't one, so we have to be more explicit in order to be sure of being understood. – James Kanze May 06 '11 at 08:57
  • 1
    @JamesKanze: I have decided that the meaning of the word should reflect what the word means. I have decided that everyone will follow this no matter what. It will be written into law at the next available opportunity. Thanks for playing. – Lightness Races in Orbit May 06 '11 at 08:59
2

hash_map is a part of STL, but not a part of Standard C++(C++11). And there is a similar implementation of hash_map named "std::unordered_map" in standard C++: http://www.cplusplus.com/reference/unordered_map/unordered_map/

Currently, GNU C++ and MSVC++ have implemented hash_map for compiler extension, as well as Boost. Meanwhile, SGI has its own implementation for hash_map. You can refer to http://www.sgi.com/tech/stl/hash_map.html for details.

holybiner
  • 61
  • 2
-1

No...Hash_map is not part of the STL standard.

Raghuram
  • 3,937
  • 2
  • 19
  • 25