stringstream:为什么这段代码不返回 4?
stringstream: Why isn't this code returning 4?
#include <iostream>
#include <sstream>
using namespace std;
int get_4()
{
char c = '4';
stringstream s(ios::in);
s << c;
int i;
s >> i;
return i;
}
int main()
{
cout << get_4() << endl;
}
转换对我不起作用。如果我将字符"4"或字符数组 {'4','\0'} 写入字符串流,然后将其读出到 int i,则不会返回 4。上面的代码有什么问题?
因为您将stringstream
设置为仅输入 - 没有输出。
如果您在尝试提取int
后检查fail()
位,您会发现它不起作用:
s >> i;
bool b = s.fail();
if( b )
cerr << "WHOA DOGGIE! WE BLOWED UPn";
在代码中,更改:
stringstream s(ios::in);
自:
stringstream s;
相关文章:
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- 为什么C++程序在太大时返回代码而不是答案?
- bash脚本在使用popen()时不返回代码以调用C ++程序
- 如何在C++中执行命令并获取命令的返回代码标准输出和标准
- NMAKE:致命错误 U1077:cl.exe 和 nmake.exe 返回代码'0x2'
- DoModal 主应用程序随机返回 -1 代码
- C++线程中调用 std::terminate 时程序返回代码
- IMMDevice::激活缺少接口的返回代码
- 退出应用并在 MFC 中返回代码
- 什么是线程锁返回代码值
- 引发异常与返回代码
- 为什么我的shell只检测0-255之间的返回代码
- waitpid/wexitstatus 返回 0 而不是正确的返回代码
- linux下的Boost Semaphores和EINTR返回代码
- 从std::thread获取返回代码
- 从本地类的 C++ 析构函数访问返回代码
- DLL 注册返回代码0x80029c4a
- 定义D3DERR_INVALIDCALL(D3D11CreateDevice的返回代码)的位置
- Windows 错误消息返回代码而不是消息
- 如何在DdeCallback函数中实现返回代码