std::stringstream 奇怪的行为

发布于 2024-07-15 08:28:17 字数 1722 浏览 9 评论 0原文

一些背景信息,对于家庭作业,我必须使用二叉树编写波兰表示法计算器,为此,我必须解析命令行输入,以便它能够正确构建二叉树,然后对其进行检查以给出有效答案输入的数学表达式。

对于解析,我使用了 std::stringstream,这样我就可以轻松地将我收到的 std::string 转换为有效的浮点数(或整数、双精度)。 我遇到的问题是以下代码,其中显示了错误以及我如何解决该问题。 我希望有人能够告诉我我是否做错了什么并且 .clear() 不正确,或者这是标准库处理此特定输入的方式中的错误(仅发生在 +和 -)。

#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string mystring("+");
    int num;
    char op;

    std::stringstream iss(mystring);
    iss >> num;

    // Seems it is not a number 
    if (iss.fail()) {
            // This part does not work as you would expect it to

            // We clear the error state of the stringstream
            iss.clear();
            std::cout << "iss fail bit: " << iss.fail() << std::endl;
            iss.get(op);
            std::cout << "op is: " << op << " iss is: " << iss.str() << std::endl;
            std::cout << "iss fail bit: " << iss.fail() << std::endl;

            // This however works as you would expect it to
            std::stringstream oss(iss.str());
            std::cout << "oss fail bit: " << oss.fail() << std::endl;
            oss.get(op);
            std::cout << "op is: " << op << " oss is: " << oss.str() << std::endl;
            std::cout << "oss fail bit: " << oss.fail() << std::endl;

    } else {
            // We got a number
    }   
}

程序的示例输出:

iss fail bit: 0
op is:  iss is: +
iss fail bit: 1
oss fail bit: 0
op is: + oss is: +
oss fail bit: 0

也许你们会看到我错过的东西,或者如果这确实是超出我的程序的错误,在这种情况下,将不胜感激有关在哪里报告此问题的指示。

Some background information, for a homework assignment I had to write a polish notation calculator using binary trees, for this to work I had to parse command line input so that it would properly build the binary tree and then go over it to give a valid answer to the mathematical expression that was entered.

For the parsing I used a std::stringstream so that I would easily be able to convert the std::string I was handed into a valid float (or integer, double). The issue I ran across was the following code, which has the error showcased and how I solved the issue. I was hoping that somebody where would be able to tell me if I was doing something wrong and .clear() is not correct, or if this is a bug in the standard library in the way it handles this particular input (only happens for + and -).

#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string mystring("+");
    int num;
    char op;

    std::stringstream iss(mystring);
    iss >> num;

    // Seems it is not a number 
    if (iss.fail()) {
            // This part does not work as you would expect it to

            // We clear the error state of the stringstream
            iss.clear();
            std::cout << "iss fail bit: " << iss.fail() << std::endl;
            iss.get(op);
            std::cout << "op is: " << op << " iss is: " << iss.str() << std::endl;
            std::cout << "iss fail bit: " << iss.fail() << std::endl;

            // This however works as you would expect it to
            std::stringstream oss(iss.str());
            std::cout << "oss fail bit: " << oss.fail() << std::endl;
            oss.get(op);
            std::cout << "op is: " << op << " oss is: " << oss.str() << std::endl;
            std::cout << "oss fail bit: " << oss.fail() << std::endl;

    } else {
            // We got a number
    }   
}

Sample output from the program:

iss fail bit: 0
op is:  iss is: +
iss fail bit: 1
oss fail bit: 0
op is: + oss is: +
oss fail bit: 0

Maybe you guys will see something I missed, or if this is indeed a bug higher up beyond my program, in which case pointers as to where to report this would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彩虹直至黑白 2024-07-22 08:28:17

当你说:

  iss.clear();
  std::cout << "iss fail bit: " << iss.fail() << std::endl;
  iss.get(op);

你正在尝试阅读已经读过的内容。 您需要重置流读取指针:

  iss.clear();
  iss.seekg(0);    // start again
  std::cout << "iss fail bit: " << iss.fail() << std::endl;
  iss.get(op);

When you say:

  iss.clear();
  std::cout << "iss fail bit: " << iss.fail() << std::endl;
  iss.get(op);

you are trying to read something that has already been read. You need to reset the streams read pointer:

  iss.clear();
  iss.seekg(0);    // start again
  std::cout << "iss fail bit: " << iss.fail() << std::endl;
  iss.get(op);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文