Whether you're writing business applications, website backend code, games, or nearly any other modern application, it's likely that you need to quickly access large amounts of data and perform calculations on this data. For instance, at Advertising.com we have to rapidly perform complex mathematical calculations on huge amounts of in-memory data so that we can choose the most relevant ads to show website visitors. Of course, there are many different profiling and optimization techniques you can use once your application is completed. But even if you believe that "premature optimization is the root of all evil," you should begin thinking about application performance as early as possible. Why? Because the proper choice of data structures is a make-or-break architectural decision for performance-critical applications.
Fortunately for C++ programmers, the C++ Standard Template Library provides a wealth of commonly used data structures that can be easily incorporated into most programs. Many of the simpler data structures are familiar: The performance trade-offs of vectors, linked lists, and queues are taught in introductory computer-science courses. It's also straightforward to understand and use sorted associative containers like maps and sets without knowing what's going on under the hood, particularly if it's been a while since you've looked at the details of Red/Black Tree algorithms that typically underlie them.
But understanding implementation details and design trade-offs is critical for using some of the most efficient and powerful data structures available in STL distributionshash-based structures that include
hash_multiset. Although these structures are not yet part of the C++ Standard, they are included in many STL packages, including the SGI version that ships with most Linux distributions. They will also be included in TR1 under the slightly different names of
unordered_multiset. The examples in this article use the SGI STL implementation. But even if you use a different STL, the implementations are likely to be similar.
Each of these data structures provides functionality similar to their nonhash-based counterparts:
hash_mapsstore key/value pairs that have unique keys.
hash_setsstore unique values.
hash_multimaps store key/value pairs with nonunique keys.
hash_multisets store nonunique values.
For many applications, the speedup that can be gained by using, for instance,
hash_maps instead of regular maps is significant. In theory, hash-based containers can provide constant-time insertion, deletion, and access to any values in the containers. In contrast, the Red/Black Trees that power STL's maps, sets, multimaps, and multisets require
O(log n) time for the equivalent operations.