如何创建一个适配器,使用键的谓词根据过滤后的键返回映射值?
例如:
std::map<int,int> map_obj;
const int match_value = 0xFF00;
for(auto& i : map_obj | filtered_key_map_values([match_value](key_type& x){ return (x & match_value) > 0; } | indirected )
{
std::copy<typeof(i)>(std::cout," ,");
}
我建议使用Live On Coliru
#define BOOST_RESULT_OF_USE_DECLTYPE
#include <boost/range/adaptors.hpp>
using namespace boost::adaptors;
#include <iostream>
int main()
{
std::map<int, std::string> const map_obj {
{ 0x0001, "one" },
{ 0x0002, "two" },
{ 0x0003, "three" },
{ 0x0404, "four" },
{ 0x0005, "five" },
};
const int match_value = 0xFF00;
for(auto& v : map_obj
| filtered([=](std::pair<const int, std::string> const& p)->bool { return (p.first & match_value) != 0; })
| map_values)
{
std::cout << v << "n";
}
}