簡體   English   中英

C ++ 11 regex_token_iterator

[英]C++11 regex_token_iterator

嗯......我以為我理解正則表達式,我認為我理解迭代器,但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"));
   ...
}

我不明白以下輸出如何:

Quick
brown
fox.

正在由上面的std :: copy()函數創建。 我看不到循環,所以我對迭代的發生方式感到困惑。 換句話說,如何生成多行輸出?

std::copy將輸入范圍內的元素std::copy到輸出范圍。 在您的程序中,輸入范圍是使用正則表達式分隔符提取的三個標記。 這些是打印到輸出的三個單詞。 輸出范圍是ostream_iterator ,它簡單地獲取給定的每個元素,並將元素寫入輸出流。

如果您使用調試器單步執行std::copy ,您將看到它循環輸入范圍的元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM