避免运算符>>在空格处打破输入

Ove*_*owh 2 c++ string operator-overloading

我正在超载这个operator>>功能.它应该在输入中使用一个字符串,需要一些空格,在空格处爆炸字符串并执行与该主题无关的其他操作.

我有这个代码:

std::istream& operator>>(std::istream &in, Foo &f) {
    std::string str;
    in >> str;
    std::cout << "str = " << str << std::endl; // for testing
    // ...
    return in;
}
Run Code Online (Sandbox Code Playgroud)

假设将此字符串(复数)作为输入:

3 + 2i
Run Code Online (Sandbox Code Playgroud)

std::cout功能仅打印3.我试图把旗帜std::noskipws,但问题仍然存在.

有什么方法可以解决这个问题吗?

Roh*_*ain 5

使用std::getline函数读取完整的输入行:

std::getline(in, str);
Run Code Online (Sandbox Code Playgroud)