C++循环中的 istringstream 不改变其值
我对 C++ 相当陌生,并且遇到了一个问题,通过单独搜索我找不到解决方案。
问题是,为什么 istringstream 在下面的循环中永远不会改变它的值?
它从 dirs[ 0 ] 和 dirs[ 1 ] 获取值,并且不会将它们更改为递增的 int i。 顺便提一句。 dirs[ i ] 和 dirs [ i + 1 ] 中的值存储为十六进制值(例如 0F9C8924)。
下面是我的最新设置,我尝试了其他几种方法但没有成功,例如在循环内使用 istringstream 并使用 ios_base::trunc 等。
还有 dirs[ i ]
等。确实有不同的值并且可以正确读取,但是当尝试通过 istringstream 将字符串十六进制转换为无符号整数时,它永远不会采用新值。
unsigned int f;
unsigned int t;
istringstream ss;
istringstream ss2;
for( int i = 0; i < count; i+=3 ) {
ss.clear();
ss2.clear();
ss.str( dirs[ i ] );
ss2.str( dirs[ i + 1 ] );
ss >> f;
ss2 >> t;
// do something else with dirs[ i + 3 ], not relevant
}
count
和 dirs
是全局变量,count
在另一个函数中增加,它是 dirs
中值的计数。
如果之前有人问过这个问题,我很抱歉,但我找到的解决方案对我来说并不适用。 例如 ss.clear()
或 while( ss >> f )
提前感谢您提供的任何帮助。
I'm fairly new to c++ and have encountered a problem where by searching alone I couldn't find a solution.
The problem is, why does the istringstream never changes its value inside the loop below?
It takes the value from dirs[ 0 ] and dirs[ 1 ] and never changes them to the increasing int i.
Btw. the values in dirs[ i ] and dirs [ i + 1 ] are stored as hex values (e.g. 0F9C8924).
Below is my latest setup, i've tried several other ways but with no success, for example having istringstream inside the loop and with ios_base::trunc and whatsoever.
Also dirs[ i ]
etc. DO have different values and are read correctly, but when trying to make the string hex into a unsigned int via istringstream it never takes the new values.
unsigned int f;
unsigned int t;
istringstream ss;
istringstream ss2;
for( int i = 0; i < count; i+=3 ) {
ss.clear();
ss2.clear();
ss.str( dirs[ i ] );
ss2.str( dirs[ i + 1 ] );
ss >> f;
ss2 >> t;
// do something else with dirs[ i + 3 ], not relevant
}
count
and dirs
are a global variable and count
is increased in another function, its the count of values in dirs
.
I am sorry if this has been asked before, but the solutions I found somehow didn't work for me.
Such as ss.clear()
or while( ss >> f )
Thanks in advance for any help provided.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题有点令人困惑。你的意思是输入是像“0F9C8924”这样的字符串?
如果是这样,请尝试
ss >>十六进制>> f;
.您应该在代码中编写一些错误处理,以便您知道何时以及为何会出现问题。
ss.clear()
只是盲目地清除错误标志,而没有找出它们最初设置的原因。The question is a bit confusing. Do you mean that the input is a string like "0F9C8924"?
If so, try
ss >> hex >> f;
.You should write some error handling into your code so that you know when and why things are going wrong.
ss.clear()
just blindly clears error flags without ever finding out why they were set in the first place.试试这个:
我假设您认为clear()会清空流。 str() 会将一些文本设置到流中。可能最好在循环内声明字符串流。这意味着它们在循环结束时被销毁,并在每次进入循环时重新创建。然后你可以用构造函数初始化它们。
Try this:
I assume you thought clear() would empty the stream. And str(<string>) would set some text into the stream. Probably best to declare the stringstream inside the loop. This means they are destroyed at the end of the loop and re-created each time the loop is entered. Then you can just initialize them with there constructor.
这将打印出十六进制字符串的十进制值。这是你想要的吗?
This prints out the decimal values of the hex strings. Is this what you want?