istringstream没有更改c++
istringstream not changing c++
本文关键字:c++ istringstream 更新时间:2023-10-16
我写了一些代码来显示我在另一个程序中转换一些输入字符串时遇到的问题。
#include <iostream>
#include <sstream>
#include <bitset>
using namespace std;
int main()
{
string tempStr;
unsigned long tempVal;
istringstream tempIss;
bitset<32> tempBitset;
// Input the first word in hex and convert to a bitset
cout << "enter first word in hex : ";
cin >> tempStr;
tempIss.str(tempStr);
cout << "word2 tempIss : " << tempIss.str() << endl;
tempIss >> hex >> tempVal;
cout << "word2 tempVal : " << (int)tempVal << endl;
tempBitset = tempVal;
cout << "word1 tempBitset: " << tempBitset.to_string() << endl;
// Input the second word in hex and convert to a bitset
cout << "enter second word in hex : ";
cin >> tempStr;
tempIss.str(tempStr);
cout << "word2 tempIss : " << tempIss.str() << endl;
tempIss >> hex >> tempVal;
cout << "word2 tempVal : " << (int)tempVal << endl;
tempBitset = tempVal;
cout << "word2 tempBitset: " << tempBitset.to_string() << endl;
return 0;
}
我的问题是,当tempIss的值似乎随着tempIss.str(tempStr);
函数的变化而变化时,下面的tempIss >> hex >> tempVal;
似乎使用了tempIss!?的旧值!?
谢谢。
tempIss.str(tempStr);
这只是设置了意图缓冲区的内容。但是,它不会重置以前操作中可以在流上设置的错误标志。
在第二次提取之前说tempIss.clear();
就可以了。
相关文章:
- 如何使用iStringstream读取和解析文本文件?
- istringstream >>运算符产生意外行为
- 在C++中使用istringstream读取输入时出错
- istringstream,num1 和 num2 在这里发生了什么?
- 来自 istringstream 的五倍输出
- 是否可以在不指定类型的情况下从 istringstream 转到向量?
- 使用 string_view 构造 istringstream 无法编译
- 有没有办法我可以从 istringstream 中读取两个字符
- istringstream将字符串放回输入并再次读取
- 为什么 istringstream 在流的末尾附加'-1'字符?
- 字符串向量到istringstream
- istringstream-function string_to_int不能接受C-STR,为什么
- 基于循环的C ISTRINGSTREAM范围没有开始成员
- 无法使用 istringstream 从.txt文件中读取
- 为什么当我将 std::locale 设置为 "zh_CN.UTF-8" 时 std::istringstream 失败?
- 修改 iStringStream 中的变量数
- NDK r16b std::istringstream 的行为不符合预期
- 如何在 c++ 中使用 istringstream
- 有没有办法重写一个返回指向 istringstream 的指针的函数,而不是返回一个引用?
- 这一行是怎么回事: istringstream is( line );