这个:
#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 位类型,还是必须使用中间存储?
您必须使用中间类型或自己进行解析。所有 char 类型(char、signed char 和 unsigned char)都被视为文本元素,而不是整数。int8_t 可能只是其中之一的 typedef,这就是您的代码失败的原因。
笔记:
| 归档时间: |
|
| 查看次数: |
978 次 |
| 最近记录: |