mirror of
https://github.com/boostorg/unordered.git
synced 2025-07-31 20:04:29 +02:00
Making use of the new quickbook import feature.
[SVN r3621]
This commit is contained in:
@@ -30,39 +30,12 @@ Alternatively, you might wish to use a different equality function. If so, make
|
||||
sure you use a hash function that matches it. For example, a
|
||||
case-insensitive dictionary:
|
||||
|
||||
struct iequal_to
|
||||
: std::binary_function<std::string, std::string, bool>
|
||||
{
|
||||
bool operator()(std::string const& x,
|
||||
std::string const& y) const
|
||||
{
|
||||
return boost::algorithm::iequals(x, y);
|
||||
}
|
||||
};
|
||||
[import src_code/insensitive.cpp]
|
||||
[case_insensitive_functions]
|
||||
[case_insensitive_dictionary]
|
||||
|
||||
struct ihash
|
||||
: std::unary_function<std::string, bool>
|
||||
{
|
||||
bool operator()(std::string const& x) const
|
||||
{
|
||||
std::size_t seed = 0;
|
||||
|
||||
for(std::string::const_iterator it = x.begin();
|
||||
it != x.end(); ++it)
|
||||
{
|
||||
boost::hash_combine(seed, std::tolower(*it));
|
||||
}
|
||||
|
||||
return seed;
|
||||
}
|
||||
};
|
||||
|
||||
struct word_info {
|
||||
// ...
|
||||
};
|
||||
|
||||
boost::unordered_map<std::string, word_info, iequal_to, ihash>
|
||||
idictionary;
|
||||
A more generic version of this example is available at:
|
||||
[@../../libs/unordered/examples/case_insensitive.hpp /libs/unordered/examples/case_insensitive.hpp]
|
||||
|
||||
[h2 Custom Types]
|
||||
|
||||
|
78
doc/src_code/insensitive.cpp
Normal file
78
doc/src_code/insensitive.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
|
||||
// Copyright 2006 Daniel James.
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/unordered_map.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
|
||||
//[case_insensitive_functions
|
||||
struct iequal_to
|
||||
: std::binary_function<std::string, std::string, bool>
|
||||
{
|
||||
bool operator()(std::string const& x,
|
||||
std::string const& y) const
|
||||
{
|
||||
return boost::algorithm::iequals(x, y);
|
||||
}
|
||||
};
|
||||
|
||||
struct ihash
|
||||
: std::unary_function<std::string, bool>
|
||||
{
|
||||
bool operator()(std::string const& x) const
|
||||
{
|
||||
std::size_t seed = 0;
|
||||
|
||||
for(std::string::const_iterator it = x.begin();
|
||||
it != x.end(); ++it)
|
||||
{
|
||||
boost::hash_combine(seed, std::toupper(*it));
|
||||
}
|
||||
|
||||
return seed;
|
||||
}
|
||||
};
|
||||
|
||||
struct word_info;
|
||||
//]
|
||||
|
||||
struct word_info {
|
||||
int tag;
|
||||
explicit word_info(int t = 0) : tag(t) {}
|
||||
};
|
||||
|
||||
int main() {
|
||||
//[case_insensitive_dictionary
|
||||
boost::unordered_map<std::string, word_info, ihash, iequal_to>
|
||||
idictionary;
|
||||
//]
|
||||
|
||||
BOOST_TEST(idictionary.empty());
|
||||
|
||||
idictionary["one"] = word_info(1);
|
||||
BOOST_TEST(idictionary.size() == 1);
|
||||
BOOST_TEST(idictionary.find("ONE") != idictionary.end() &&
|
||||
idictionary.find("ONE") == idictionary.find("one"));
|
||||
|
||||
idictionary.insert(std::make_pair("ONE", word_info(2)));
|
||||
BOOST_TEST(idictionary.size() == 1);
|
||||
BOOST_TEST(idictionary.find("ONE") != idictionary.end() &&
|
||||
idictionary.find("ONE")->first == "one" &&
|
||||
idictionary.find("ONE")->second.tag == 1);
|
||||
|
||||
idictionary["One"] = word_info(3);
|
||||
BOOST_TEST(idictionary.size() == 1);
|
||||
BOOST_TEST(idictionary.find("ONE") != idictionary.end() &&
|
||||
idictionary.find("ONE")->first == "one" &&
|
||||
idictionary.find("ONE")->second.tag == 3);
|
||||
|
||||
idictionary["two"] = word_info(4);
|
||||
BOOST_TEST(idictionary.size() == 2);
|
||||
BOOST_TEST(idictionary.find("two") != idictionary.end() &&
|
||||
idictionary.find("TWO")->first == "two" &&
|
||||
idictionary.find("Two")->second.tag == 4);
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user