Hmm... I thought I understood regexes, and I thought I understood iterators, but C++11's regex implementation has me puzzled...
One area I don't understand: Reading about regex token iterators, I came across the following sample code:
#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 don't understand how the following output:
Quick
brown
fox.
is being created by the std::copy() function above. I see no loop, so I am puzzled as how the iteration is occurring. Or put another way, how is more than one line of output being generated?
std::copy
copies elements from an input range into an output range. In your program, the input range is the three tokens extracted using the regular expression delimiter. These are the three words that are printed to the output. The output range is ostream_iterator
which simply takes each element it is given and writes the element to an output stream.
If you step through std::copy
using your debugger, you will see that it loops over the elements of the input range.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With