c++ - 为什么提取运算符不起作用?

标签 c++

我在 Google 的 Code Jam 中尝试一些问题。对于问题Store Credit ,我在 C++ 中有以下代码:

    if (input.is_open()) {
    getline(input, line);
    ss << line;
    ss >> nCases;

    for (int i = 0; i < nCases; i++) {
        getline(input, line);
        ss << line;
        ss >> credit;
        cout << credit << endl;

        getline(input, line);
        ss << line;
        ss >> nItems;
        cout << nItems << endl;
        int list[nItems];
    }

input 是文本文件(所有内容都已正确初始化),line 是一个字符串变量,用于保存从文本文件中新提取的行,而 ss 是一个字符串流。 nCasescreditnItems 只是 int 变量。让我感到困惑的是,当我得到 nCases 时,提取运算符如何按预期工作,但在我尝试检索 credit 的值时停止工作n项.

最佳答案

而不是 ss << line; , 用 ss.str(line); ss.clear(); 重置流

关于c++ - 为什么提取运算符不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5840972/

相关文章:

c++ - 安装opencv后如何安装OpenCV_contrib模块

C++ 我必须为每个源文件包含标准库吗?

c++ - 在 Ubuntu 12.04 上链接 - 对 `clock_gettime@GLIBC_2.17' 的 undefined reference

c++ - 嵌套类中用户定义的转换运算符

c++ - 访问 char* 的元素并检查值 C++

c++ - 随机化字符数组以显示随 secret 码

c++ - 窗口不活动警报

c++ - 数组初始化的时间复杂度是多少?

c++ - 如何在 C++ 中重复访问数组和 vector 的元素?

c++ - 引用 std::atomic<bool> 的已删除函数错误