【问题标题】:Stringstream clearing trouble字符串流清除麻烦
【发布时间】:2015-02-09 21:59:44
【问题描述】:

我正在使用stringstream 将数字字符串转换为整数。我不知道为什么后面的代码不起作用。有人可以向我解释为什么我总是得到相同的 tmp 变量值吗?

#include <fstream>
#include <string>
#include <sstream>
#include <cctype>

int main() {
    std::ifstream input("input.txt");
    std::ofstream output("output.txt");
    std::string str = "", line;
    std::stringstream ss;
    int tmp;
    while (std::getline(input, line)) {
        for (int i = 0, l = line.size(); i < l; i++) {
            if (isdigit(line[i]))
                str += line[i];
        }
        ss << str;
        // gets int from stringstream
        ss >> tmp;
        output << str << ' ' << tmp << std::endl;
        str = "";
        // stringstream clearing
        ss.str("");

    }

    return 0;
}

【问题讨论】:

  • resetting a stringstream 的可能重复项
  • @inetknght 与 ss.clear() 它可以工作。但为什么呢?
  • Don't use while(!input.eof())。相反,将循环更改为while (std::getline(input, line)) {
  • @NikitaShelimov clear() 重置位置指示器和任何错误标志

标签: c++ string stl stringstream


【解决方案1】:

之后

ss >> tmp;

ss 在 EOF。在那之后没有任何阅读工作。可以加一行

ss.clear();

之后

ss.str("");

清除其内部状态。它将开始工作。我使用了if 语句来检验假设。

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cctype>

int main() {
    std::ifstream input("input.txt");
    std::string str = "", line;
    std::stringstream ss;
    int tmp;
    while (std::getline(input, line)) {
        for (int i = 0, l = line.size(); i < l; i++) {
            if (isdigit(line[i]))
                str += line[i];
        }
        ss << str;
        // gets int from stringstream
        ss >> tmp;
        std::cout << str << ' ' << tmp << std::endl;
        str = "";
        // stringstream clearing

        if (ss.eof())
        {
           std::cout << "ss is at eof\n";
        }

        ss.str("");
        ss.clear();

    }

    return 0;
}

【讨论】:

  • 非常感谢!我得到它。 :)
【解决方案2】:

要重置std::stringstream,您必须首先使用std::basic_stringstream::str 设置缓冲区的内容,然后使用std::basic_istream::seekg 重置输入位置,给出

ss.str(str);
ss.seekg(0);
ss >> tmp;

【讨论】:

    【解决方案3】:

    作为@R Sahu 提到的在每次迭代中必须清除stringstream 的方法的替代方法,您可以在while 循环中声明std::stringstream ss。这样,您在每次迭代结束时销毁变量,并在每次执行 while 循环时创建一个新变量。

    #include <fstream>
    #include <string>
    #include <sstream>
    #include <cctype>
    
    int main()
    {
        std::ifstream input("input.txt");
        std::ofstream output("output.txt");
        std::string line;
    
        while (std::getline(input, line))
        {
            std::string str;
            std::stringstream ss;
            int tmp;
    
            for (int i = 0; i < line.size(); i++)
            {
                if (isdigit(line[i]))
                    str += line[i];
            }
            ss << str;
            // gets int from stringstream
            ss >> tmp;
            output << str << " " << tmp << std::endl;
        }
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-11
      • 2020-09-16
      • 2021-05-15
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多