-1

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

As an example:

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

1 回答 1

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";
    }
}
于 2014-04-29T07:52:23.243 回答