如何创建一个适配器,该适配器将使用键的谓词基于过滤的键返回映射值

时间:2014-04-29 06:16:12

标签: c++ boost map boost-range

如何使用键的谓词创建一个基于过滤键返回映射值的适配器?

举个例子:

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

1 个答案:

答案 0 :(得分:0)

这是我建议的版本 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";
    }
}