C++循环中的 istringstream 不改变其值

发布于 2024-11-07 12:28:10 字数 1001 浏览 0 评论 0原文

我对 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
}

countdirs 是全局变量,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烂柯人 2024-11-14 12:28:10

问题有点令人困惑。你的意思是输入是像“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.

人生百味 2024-11-14 12:28:10

试试这个:

unsigned int f;
unsigned int t;
for( int i = 0; i < count; i+=3 )
{
    istringstream ss1( dirs[ i ] );
    istringstream ss2( dirs[ i + 1 ] );

    ss1 >> f;
    ss2 >> t;

    // do something else with dirs[ i + 3 ], not relevant
}

我假设您认为clear()会清空流。 str() 会将一些文本设置到流中。可能最好在循环内声明字符串流。这意味着它们在循环结束时被销毁,并在每次进入循环时重新创建。然后你可以用构造函数初始化它们。

Try this:

unsigned int f;
unsigned int t;
for( int i = 0; i < count; i+=3 )
{
    istringstream ss1( dirs[ i ] );
    istringstream ss2( dirs[ i + 1 ] );

    ss1 >> f;
    ss2 >> t;

    // do something else with dirs[ i + 3 ], not relevant
}

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.

森林很绿却致人迷途 2024-11-14 12:28:10

这将打印出十六进制字符串的十进制值。这是你想要的吗?

#include <sstream>
#include <iostream>
using std::istringstream;
unsigned int f;
unsigned int t;
const char * dirs[12] = { 
    "0x15", "0x16", "dummy", 
    "0x11", "0x12", "dummy",
    "0x115", "0x116", "dummy",
    "0x111", "0x112", "dummy"
    };
unsigned int count = sizeof(dirs) / sizeof(const char *);
int main(int, char **) {
    for( unsigned int i = 0; i < count; i+=3 )
    {
        istringstream ss1( dirs[ i ] );
        istringstream ss2( dirs[ i + 1 ] );

        ss1 >> std::hex >> f;
        ss2 >> std::hex >> t;
        std::cout << "f is " << f << std::endl;
        std::cout << "t is " << t << std::endl;

        // do something else with dirs[ i + 2 ], not relevant
    }
    std::cin.get();
    return 0;
}

This prints out the decimal values of the hex strings. Is this what you want?

#include <sstream>
#include <iostream>
using std::istringstream;
unsigned int f;
unsigned int t;
const char * dirs[12] = { 
    "0x15", "0x16", "dummy", 
    "0x11", "0x12", "dummy",
    "0x115", "0x116", "dummy",
    "0x111", "0x112", "dummy"
    };
unsigned int count = sizeof(dirs) / sizeof(const char *);
int main(int, char **) {
    for( unsigned int i = 0; i < count; i+=3 )
    {
        istringstream ss1( dirs[ i ] );
        istringstream ss2( dirs[ i + 1 ] );

        ss1 >> std::hex >> f;
        ss2 >> std::hex >> t;
        std::cout << "f is " << f << std::endl;
        std::cout << "t is " << t << std::endl;

        // do something else with dirs[ i + 2 ], not relevant
    }
    std::cin.get();
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文