2014-04-10 6 views
2

boost::adaptors::filtered에 문제가 있습니다. 불행하게도 문제부스트 필터링 된 어댑터 컴파일

struct IsRegex { 
    IsRegex() {} // filter_iterator requires default constructible predicate 
    explicit IsRegex(const boost::regex &rx) : m_rx(rx) {} 
    IsRegex(const IsRegex &isRegex) : m_rx(isRegex.m_rx) {} 
    void swap(IsRegex &isRegex) { std::swap(m_rx, isRegex.m_rx); } 
    IsRegex& operator=(IsRegex isRegex) { swap(isRegex); return *this; } 

    bool operator() (const std::string &str) const { 
    return boost::regex_match(str, m_rx); 
    } 
    boost::regex m_rx; 
}; 

int main() 
{ 
    std::string foo[] = {"0ii", "22", "48", "555", "15", "ab"}; 
    typedef std::list<std::string> Container; 
    Container bar((foo), foo+5); 
    const boost::regex rx(("\\d{2}")); 
    IsRegex isRegex((rx)); 
    Container::iterator it 
     = boost::max_element(bar | boost::adaptors::filtered(isRegex)); 
} 

을 보여주는에 대한 샘플이있다, 나는이 문제의 원인과 어떻게 그것을 해결하기 위해 무엇

In function ‘int main()’: 
error: conversion from 
‘boost::filter_iterator< IsRegex, std::_List_iterator<std::string> >’ 
to non-scalar type 
‘std::_List_iterator<std::string>’ 
requested 

있어?

답변

3

당신은 .base()와 적응 반복자에서 소스 반복자를 매핑 할 :

또한
Container::iterator it = 
    boost::max_element(bar | boost::adaptors::filtered(isRegex)) 
    .base(); 

, 나는 그것이 무엇을 의미하는지 말하여 술어의 이름을 수정 제안 할 수 있습니다 :

#include <boost/range/adaptors.hpp> 
#include <boost/range/algorithm.hpp> 
#include <list> 

using namespace boost::adaptors; 

struct IsMatch { 
    IsMatch(const boost::regex &rx = boost::regex()) : m_rx(rx) {} 

    bool operator() (const std::string &str) const { 
     return boost::regex_match(str, m_rx); 
    } 
    private: 
    boost::regex m_rx; 
}; 

int main() 
{ 
    std::string foo[] = {"0ii", "22", "48", "555", "15", "ab"}; 

    typedef std::list<std::string> Container; 
    Container const bar(foo, foo+5); 

    IsMatch isMatch(boost::regex("\\d{2}")); 

    Container::const_iterator it = boost::max_element(bar | filtered(isMatch)).base(); 
}