如何在 boost::circular 缓冲区上使用 boost::regex?

How can I use boost::regex on a boost::circular buffer?

我正在 boost::circular_buffer 中捕获数据,现在想对内容执行正则表达式搜索,但我在 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,如果有区别的话。

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

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...
    }
}