U00*_*07D 5 c++ regex copy c++11
嗯......我以为我理解正则表达式,我认为我理解迭代器,但C++ 11的正则表达式实现让我感到困惑......
我不明白的一个领域:阅读有关正则表达式令牌迭代器的内容,我遇到了以下示例代码:
#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"));
...
}
Run Code Online (Sandbox Code Playgroud)
我不明白以下输出如何:
Quick
brown
fox.
Run Code Online (Sandbox Code Playgroud)
正在由上面的std :: copy()函数创建.我看不到循环,所以我对迭代的发生方式感到困惑.换句话说,如何生成多行输出?
std::copy将元素从输入范围复制到输出范围。在您的程序中,输入范围是使用正则表达式分隔符提取的三个标记。这是打印到输出的三个单词。输出范围只是ostream_iterator简单地获取给定的每个元素并将该元素写入输出流。
如果您使用调试器逐步执行std::copy,您将看到它循环遍历输入范围的元素。