c++ - 如何存储循环缓冲区迭代器的中间值?

标签 c++ boost-regex circular-buffer

我在 boost 循环缓冲区上使用 boost 正则表达式,想“记住”匹配发生的位置,最好的方法是什么?我尝试了下面的代码,但“结束”似乎一直存储相同的值!例如,当我尝试从之前的“结束”遍历到最近的“结束”时,它不起作用!

  boost::circular_buffer<char> cb(2048);
  typedef boost::circular_buffer<char>::iterator  ccb_iterator;
  boost::circular_buffer<ccb_iterator> cbi(4); 

  //just fill the whole cbi with cb.begin()  
  cbi.push_back(cb.begin());
  cbi.pushback(cb.begin());
  cbi.pushback(cb.begin());
  cbi.pushback(cb.begin());


 typedef regex_iterator<circular_buffer<char>::iterator> circular_regex_iterator;

 while (1)
{
  //insert new data in circular buffer (omitted)
  //basically reads data from file and pushes it back to cb

  boost::circular_buffer<char>::iterator    start,end;  

 circular_regex_iterator regexItr(
        cb.begin(), 
        cb.end() , 
         re, //expression of the regular expression
         boost::match_default | boost::match_partial); 
    circular_regex_iterator last;

    while(regexItr != last)
    {

            if((*regexItr)[0].matched == false)
           {
               //partial match      
               break;
            }
        else
        {
           // full match:
           start = (*regexItr)[0].first;
           end = (*regexItr)[0].second; 

             //I want to store these "end" positions to to use later so that I can 
             //traverse the buffer between these positions (matches).  

            //cbi stores positions of these matches, but this does not seem to work!                 
             cbi.push_back(end);    

            //for example, cbi[2] --> cbi[3] traversal works only first time this 
            //loop is run!
        }

        ++regexItr;
    }

最佳答案

这与其说是一个尝试重建您正在做的事情的答案,不如说是一个答案。我正在制作一个从字符串初始化的简单循环缓冲区,然后遍历该缓冲区的正则表达式匹配项并打印匹配的范围。似乎一切正常。

我不建议将范围本身存储在循环缓冲区中;或者至少范围应该成对存储。

这是我的测试代码:

#include <iostream>
#include <string>
#include <boost/circular_buffer.hpp>
#include <boost/regex.hpp>
#include "prettyprint.hpp"

typedef boost::circular_buffer<char> cb_char;
typedef boost::regex_iterator<cb_char::iterator> cb_char_regex_it;

int main()
{
  std::string sample = "Hello 12 Worlds 34 ! 56";
  cb_char cbc(8, sample.begin(), sample.end());

  std::cout << cbc << std::endl;    // (*)

  boost::regex expression("\\d+");  // just match numbers

  for (cb_char_regex_it m2, m1(cbc.begin(), cbc.end(), expression); m1 != m2; ++m1)
  {
    const auto & mr = *m1;
    std::cout << "--> " << mr << ", range ["
              << std::distance(cbc.begin(), mr[0].first) << ", "
              << std::distance(cbc.begin(), mr[0].second) << "]" << std::endl;
  }
}

(这使用 pretty printer 打印原始循环缓冲区;您可以删除标记为 (*) 的行。)


更新:这是存储匹配项的可能方法:

typedef std::pair<std::size_t, std::size_t> match_range;
typedef std::vector<match_range>            match_ranges;

/* ... as before ... */

  match_ranges ranges;

  for (cb_char_regex_it m2, m1(cbc.begin(), cbc.end(), expression); m1 != m2; ++m1)
  {
    const auto & mr = *m1;

    ranges.push_back(match_range(std::distance(cbc.begin(), mr[0].first), std::distance(cbc.begin(), mr[0].second)));

    std::cout << "--> " << mr << ", range " << ranges.back() << std::endl;
  }

  std::cout << "All matching ranges: " << ranges << std::endl;

关于c++ - 如何存储循环缓冲区迭代器的中间值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7063984/

相关文章:

c++ boost迭代器抛出断言并且程序终止

ios - 核心音频 : Float32 to SInt16 conversion artefacts

c++ - 映射分支瓦片路径

c++ - 数组一直只返回最后一个元素。 [C/阿杜伊诺]

c++ - 二进制文件大小大于预期的 C++

c++ - 将两个正则表达式结果连接到一个输出字段中,但一次只能有一个

java - 是否有循环队列的非并发(ArrayBlockingQueue 除外)标准实现?

c++ - 如何在 C++ 中使用指针正确设计类的层次结构

c++ - boost regex_search 找不到第一个匹配项

c++ - 如何获取匹配数组