相关疑难解决方法(0)

istringstream 十进制整数输入到 8 位类型

这个:

#include <iostream>
#include <sstream>
#include <inttypes.h>

using namespace std;

int main (void) {
    istringstream iss("123 42");
    int8_t x;
    while (iss >> x) {
        cout << x << endl;
    }
    return 0;
}  
Run Code Online (Sandbox Code Playgroud)

产生:

1
2
3
4
2
Run Code Online (Sandbox Code Playgroud)

但我想要:

123
42
Run Code Online (Sandbox Code Playgroud)

转换iss >> (int)x(我最初用 a 尝试过char)给了我“错误:二进制表达式的无效操作数('istringstream'(又名'basic_istringstream')和'int')”(clang)或“错误:'operator>>'的模糊重载" (g++)。

有没有办法将值作为数字直接读入8 位类型,还是必须使用中间存储?

c++ stringstream

5
推荐指数
1
解决办法
978
查看次数

标签 统计

c++ ×1

stringstream ×1