【发布时间】:2014-08-10 16:05:50
【问题描述】:
这个:
#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;
}
生产:
1
2
3
4
2
但我想要:
123
42
强制转换 iss >> (int)x(我最初用 char 尝试过)给了我“错误:二进制表达式的无效操作数('istringstream'(又名'basic_istringstream')和'int') " (clang) 或 "error: 'operator>>' 的重载不明确" (g++)。
有没有办法将值作为数字直接读入 8 位 类型,还是必须使用中间存储?
【问题讨论】:
-
请注意,这个输出
1 2 3 4 2 2仍然是错误的,因为2被打印了两次最后。这是因为错误的循环。你应该使用像while(stream >> item){ ... }这样的循环。 -
@Nawaz 点采取,更正。
标签: c++ stringstream