C ++ 11 regex_token_iterator

Hmm ... I thought I understood regular expressions, and I thought I understood iterators, but the implementation of the C ++ 11 regular expression puzzled me ...

One area I don’t understand: after reading about regex token iterators , I came across the following code example:

#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"));
   ...
}

I do not understand how the following conclusion:

Quick
brown
fox.

created by the std :: copy () function above. I don't see the loop, so I'm puzzled by how the iteration happens. Or else, how is more than one line of output generated?

+5
source share
1 answer

std::copy . , . , . ostream_iterator, , , .

std::copy , , .

+4

All Articles