In the previous recipe, we saw how string comparison can be optimized using hashing. After reading it, the following question may arise: can we make a container that will cache hashed values to use faster comparison?
The answer is yes, and we can do much more. We may achieve almost constant search, insertion, and removal times for elements.
Basic knowledge about C++ and STL containers are required. Reading the previous recipe will also help.
This will be the simplest of all recipes:
- All you need to do is just include the
<boost/unordered_map.hpp>
header, if you wish to use maps. If we wish to use sets, include the<boost/unordered_set.hpp>
header. - Now, you are free to use
boost::unordered_map
instead ofstd::map
andboost::unordered_set
instead ofstd::set
:
#include <boost/unordered_set.hpp> #include <string> #include <cassert> void example() { boost::unordered_set<std::string> strings; strings...