嗯..。我认为我理解正则表达式,并且我认为我理解迭代器,但是C++11的regex实现让我感到困惑……
我不理解的一个方面是:在阅读有关regex token iterators的文章时,我偶然发现了以下示例代码:
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <regex>
int main()
{
std::string text = "Quick brown fox.";
// tokenization (non-matched fragments)
// Note that regex is matched only two times: when the third value is obtained
// the iterator is a suffix iterator.
std::regex ws_re("\\s+"); // whitespace
std::copy( std::sregex_token_iterator(text.begin(), text.end(), ws_re, -1),
std::sregex_token_iterator(),
std::ostream_iterator<std::string>(std::cout, "\n"));
...
}我不明白下面的输出是怎么回事:
Quick
brown
fox.由上面的std::copy()函数创建。我看不到循环,所以我对迭代是如何发生的感到困惑。或者换一种说法,如何生成多行输出?
发布于 2012-08-28 14:06:55
std::copy将元素从输入范围复制到输出范围。在您的程序中,输入范围是使用正则表达式分隔符提取的三个标记。这是打印到输出中的三个单词。输出范围是ostream_iterator,它简单地获取给定的每个元素,并将该元素写入输出流。
如果使用调试器单步执行std::copy,您将看到它循环遍历输入范围的元素。
https://stackoverflow.com/questions/12153573
复制相似问题