如何创建一个适配器,该适配器将使用键的谓词根据筛选的键返回映射值

how to create an adaptor that will return map values based on filtered key using a predicate for key

本文关键字:适配器 谓词 筛选 映射 返回 创建 何创建 一个      更新时间:2023-10-16

如何创建一个适配器,使用键的谓词根据过滤后的键返回映射值?

例如:

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";
    }
}