Npeach
Npeach

Reputation: 55

Why don't vector elements print?

I have a regex function that parses a URL request and finds a match for an IP and port pattern. I want to push these matches into a vector and then print them out to the screen. The size of the vector prints to the screen but nothing is printed to the screen when I attempt to iterate through the vector and print the elements.

code:

    std::vector<std::string> matchVector;
    std::smatch m;
    std::regex e ("\\/([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})\\:?([0-9]{1,5})");
    while (std::regex_search (requestURL,m,e))
    {
        for (auto x:m)
            {
                std::stringstream ss;
                ss << x;
                std::string str = ss.str();
                matchVector.push_back(str);
                std::cout << "match " << str << " ";
            }
        std::cout << std::endl;
        requestURL = m.suffix().str();
    }
    std::cout << "print vector of size : " << matchVector.size()<< '\n';

    //this is where nothing prints to the screen
    for (int i =0; i < matchVector.size(); i++)
    {
        std::cout << matchVector[i];
    }

current output:

match /192.xxx.111.xxx:8080 match 192.xxx.111.xxx match 8080
print vector of size : 3

Upvotes: 1

Views: 96

Answers (1)

Paul Evans
Paul Evans

Reputation: 27577

std::cout is buffered, so it's not synchronized with what you see on the terminal. Try simply flushing std::cout after your print loop:

std::cout  << std::flush;

Upvotes: 1

Related Questions