简体   繁体   中英

Storing regular expression result in vector<string> using <regex>

I am extracting a line of text from a string using regular expressions. I am storing them in a vector to used later. My problem is when I access the text from the vector nothing comes out. I noticed when displaying the string using cout that it will not display anything unless I used endl after it. I noticed as well that the example given by C++ deliberately cout's endl to display the regex_search result http://www.cplusplus.com/reference/regex/regex_search/ .

Here's the snippet of my code in question:

while (regex_search(s, m, e)) {

     for (auto x:m){

        blocks.push_back(x);
     }

     s = m.suffix().str();
}

for (auto i: blocks){
        cout << i;
}

When I do

for (auto i: blocks){
        cout << i << endl;
}

it displays the contents.

cout may cache. endl flushes the cache as cout.flush(); Put cout.flush(); after last loop to see what output with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM