Switching from formatted to unformatted input in C++

前端 未结 1 1414
一个人的身影
一个人的身影 2021-01-26 08:56

I have an input text file. The first line has two int numbers a and b, and the second line is a string. I want to use formatted input to d

相关标签:
1条回答
  • 2021-01-26 09:27

    Yes, there is: std::ws manipulator. It skips whitespace characters until a non-whitespace is found or end of stream is reached.. It is similar to use of whitespace character in scanf format string.

    In fact, it is used by formatted input before actually starting to parse characters.

    You can use it like that:

    int x;
    std::string str;
    std::cin >> x >> std::ws;
    std::getline(std::cin, str);
    //...
    //std::vector<int> vec;
    for(auto& e: vec) {
        std::cin >> e;
    }
    std::getline(std::cin >> std::ws, str);
    
    0 讨论(0)
提交回复
热议问题