2016-11-07 97 views
1

我正在捕获boost :: circular_buffer中的数据,并且想要现在对内容执行正则表达式搜索,但我在获取boost :: regex以了解如何查看缓冲区方面遇到了一些困难。如何在boost :: circular缓冲区上使用boost :: regex?

这里的之类的事情的一个精简版,我想基于示例here做:

// Set up a pre-populated data buffer as an example 
    std::string test = "Fli<usefuldata>bble"; 
    boost::circular_buffer<char> dataBuff; 
    for (std::string::iterator it = test.begin(); it != test.end(); ++it) 
    { 
     dataBuff.push_back(*it); 
    } 

    // Set up the regex 
    static const boost::regex e("<\\w*>"); 
    std::string::const_iterator start, end; 
    start = dataBuff.begin(); // <-- error C2679 
    end = dataBuff.end(); 
    boost::match_flag_type flags = boost::match_default; 

    // Try and find something of interest in the buffer 
    boost::match_results<std::string::const_iterator> what; 
    if (regex_search(start, end, what, e, flags)) 
    { 
     // Do something - we found what we're after... 
    } 

我(很理解我认为),当我尝试编译此错误:

1>c:\projects\ProtocolBufferProcessor\ProtocolBufferProcessor.h(53): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'boost::cb_details::iterator<Buff,Traits>' (or there is no acceptable conversion) 
1>   with 
1>   [ 
1>    Buff=boost::circular_buffer<char>, 
1>    Traits=boost::cb_details::nonconst_traits<std::allocator<char>> 
1>   ] 
1>   c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\xstring(435): could be 'std::_String_iterator<_Elem,_Traits,_Alloc> &std::_String_iterator<_Elem,_Traits,_Alloc>::operator =(const std::_String_iterator<_Elem,_Traits,_Alloc> &)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char>, 
1>    _Alloc=std::allocator<char> 
1>   ] 
1>   while trying to match the argument list '(std::_String_iterator<_Elem,_Traits,_Alloc>, boost::cb_details::iterator<Buff,Traits>)' 
1>   with 
1>   [ 
1>    _Elem=char, 
1>    _Traits=std::char_traits<char>, 
1>    _Alloc=std::allocator<char> 
1>   ] 
1>   and 
1>   [ 
1>    Buff=boost::circular_buffer<char>, 
1>    Traits=boost::cb_details::nonconst_traits<std::allocator<char>> 
1>   ] 

......但除了每次运行正则表达式时,我都可以做其他任何事情,而不是从循环缓冲区中按字符创建一个std :: string字符?

我使用Boost v1.54,如果这有所作为。

+0

尝试使用自动的“开始”和“结束”变量和decltype(开始)作为一个类型参数为'什么'变量。 – Slava

+0

我试着改成'auto start = dataBuff.begin();'但是我得到了'错误C4430:缺少类型说明符 - int假定。注意:C++不支持default-int' –

+0

,严格地说auto和decltype不是必须的,你可以手动地放置适当的迭代器类型(用于cirucular缓冲区)。但这是一件麻烦事。你使用哪个编译器? – Slava

回答

1

你需要使用缓冲区的迭代器类型:

Live On Coliru

#include <boost/circular_buffer.hpp> 
#include <boost/regex.hpp> 

using buffer_t = boost::circular_buffer<char>; 

int main() { 
    // Set up a pre-populated data buffer as an example 
    std::string test = "Fli<usefuldata>bble"; 
    buffer_t dataBuff; 
    for (std::string::iterator it = test.begin(); it != test.end(); ++it) 
    { 
     dataBuff.push_back(*it); 
    } 

    // Set up the regex 
    static const boost::regex e("<\\w*>"); 

    buffer_t::const_iterator start = dataBuff.begin(); // <-- error C2679 
    buffer_t::const_iterator end = dataBuff.end(); 
    boost::match_flag_type flags = boost::match_default; 

    // Try and find something of interest in the buffer 
    boost::match_results<buffer_t::const_iterator> what; 
    if (regex_search(start, end, what, e, flags)) 
    { 
     // Do something - we found what we're after... 
    } 
} 
+0

这确实可以解决问题。非常感谢 :) –

相关问题