Pergunta

std::copy(map.begin(), map.end(), std::back_inserter(list)) this is what I am trying to achive.. take i->second items from map to list. Is there any boost iterator adopters that I can use ?

I thought transform_iterator will work. and this is how I was trying to do.

std::map<int, std::string> map;
std::list<std::string>     list;

std::copy(
    boost::make_transform_iterator(map.begin(), 
        boost::bind(&std::pair<int, std::string>::second, _1)), 
    boost::make_transform_iterator(map.end(), 
        boost::bind(&std::pair<int, std::string>::second, _1)),
    std::back_inserter(list));

I am actually applying on std::set_intersection That doesn't take a functor as function parameter So I cannot use std::transform and pass a function. (attaching actual code)

typedef std::map<khut::point::value_type, khut::diagonal> storage_type;

storage_type repo_begining;
storage_type repo_ending;

storage_type::const_iterator lower_it = repo_begining.lower_bound(diagonal.begining().y());
storage_type::const_iterator upper_it = repo_ending.lower_bound(diagonal.ending().y());

std::list<khut::diagonal> diagonals_intersecting;

std::set_intersection(lower_it, repo_begining.end(), repo_ending.begin(), upper_it, std::back_inserter(diagonals_intersecting));

Note: I cannot use C++11

Foi útil?

Solução

The problem is in value_type. std::map<int, std::string>::value_type is not std::pair<int, std::string> it is std::map<const int, std::string> So once I changed the code to

std::copy(
    boost::make_transform_iterator(map.begin(), 
        boost::bind(&std::pair<const int, std::string>::second, _1)), 
    boost::make_transform_iterator(map.end(), 
        boost::bind(&std::pair<const int, std::string>::second, _1)),
    std::back_inserter(list));

It worked

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top