使用 istreamstream 模拟 sscanf's %*s

发布于 2024-12-12 06:15:18 字数 438 浏览 0 评论 0原文

可能的重复:
sscanf() 的 C++ 替代方案

我有以下代码

sscanf(s, "%*s%d", &d);

行我如何使用 <代码> istringstream ?

我尝试了这个:

istringstream stream(s);
(stream >> d);

但由于 sscanf() 中的 *s ,它不正确。

Possible Duplicate:
C++ alternative to sscanf()

I have the following line of code

sscanf(s, "%*s%d", &d);

How would I do this using istringstream?

I tried this:

istringstream stream(s);
(stream >> d);

But it is not correct because of *s in sscanf().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

慵挽 2024-12-19 06:15:18

sscanf 一起使用的 %*s 基本上意味着忽略一个字符串(直到空格的任何字符),然后您就可以告诉它读取一个整数(%*s%d)。在这种情况下,星号 (*) 与指针无关。

因此,使用 stringstream,只需模拟相同的行为即可;读入一个字符串,在读入整数之前可以忽略该字符串。

int d;
string dummy;
istringstream stream(s);

stream >> dummy >> d;

IE。使用以下小程序:

#include <iostream>
#include <sstream>
using namespace std;

int main(void)
{
   string s = "abc 123";

   int d;
   string dummy;
   istringstream stream(s);

   stream >> dummy >> d;

   cout << "The value of d is: " << d << ", and we ignored: " << dummy << endl;

   return 0;
}

输出将是:d 的值为:123,我们忽略了:abc

The %*s used with sscanf basically means to ignore a string (any characters up until a whitespace), and then after that you're telling it to read in an integer (%*s%d). The asterisk (*) has nothing to do with pointers in this case.

So using stringstreams, just emulate the same behaviour; read in a string that you can ignore before you read in the integer.

int d;
string dummy;
istringstream stream(s);

stream >> dummy >> d;

ie. With the following small program:

#include <iostream>
#include <sstream>
using namespace std;

int main(void)
{
   string s = "abc 123";

   int d;
   string dummy;
   istringstream stream(s);

   stream >> dummy >> d;

   cout << "The value of d is: " << d << ", and we ignored: " << dummy << endl;

   return 0;
}

the output will be: The value of d is: 123, and we ignored: abc.

柏林苍穹下 2024-12-19 06:15:18

您的代码中没有指针操作。

正如 AusCBloke 所说,您需要先读取所有不需要的字符将 int 转换为 std::string。您还希望确保处理 s 的格式错误的值,例如具有任何整数的值。

#include <cassert>
#include <cstdio>
#include <sstream>

int main()
{
    char s[] = "Answer: 42. Some other stuff.";
    int d = 0;

    sscanf(s, "%*s%d", &d);
    assert(42 == d);

    d = 0;

    std::istringstream iss(s);
    std::string dummy;
    if (iss >> dummy >> d)
    {
        assert(dummy == "Answer:");
        assert(42 == d);
    }
    else
    {
        assert(!"An error occurred and will be handled here");
    }
}

There is no pointer manipulation in your code.

As AusCBloke has said, you need to read the all of the unwanted characters before the int into a std::string. You also want to ensure that you handle malformed values of s, such as those with any integers.

#include <cassert>
#include <cstdio>
#include <sstream>

int main()
{
    char s[] = "Answer: 42. Some other stuff.";
    int d = 0;

    sscanf(s, "%*s%d", &d);
    assert(42 == d);

    d = 0;

    std::istringstream iss(s);
    std::string dummy;
    if (iss >> dummy >> d)
    {
        assert(dummy == "Answer:");
        assert(42 == d);
    }
    else
    {
        assert(!"An error occurred and will be handled here");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文