Hi John,
2012/1/11 John M. Dlugosz :
Consider something like this:
themap_type::iterator search_for (int val)
{
return boost::find_if (themap|map_values, my_predicate(val));
}
...
Is there a good reason why the filtered iterator can't give up the inner
original iterator?
How do you use range adaptors in these situations?
I think this situation's better way is wrap by function template and
use boost::optional.
example:
#include <iostream>
#include <map>
#include
#include
#include
template
boost::optional
search_for(const Range& r, Predicate pred)
{
auto it = boost::find_if(r, pred);
if (it == boost::end(r))
return boost::none;
return *it;
}
template
boost::optional<typename MapRange::mapped_type>
map_search_for(const MapRange& r, Predicate pred)
{
return search_for(r | boost::adaptors::map_values, pred);
}
bool is_alice(const std::string& name)
{
return name == "Alice";
}
int main()
{
std::map m = {{3, "Alice"}, {1,"Bob"}, {4, "Carol"}};
if (boost::optionalstd::string name = map_search_for(m, is_alice)) {
std::cout << "found:" << name.get() << std::endl;
}
else {
std::cout << "not found" << std::endl;
}
}
Thanks,
========================
Akira Takahashi
blog : http://d.hatena.ne.jp/faith_and_brave/