gpt4 book ai didi

c++ - 如何格式化字符串流的输出?

转载 作者:行者123 更新时间:2023-11-28 03:07:06 62 4
gpt4 key购买 nike

我正在尝试从字符串流中获取特定数据。我正在将此数据从文件读取到字符串流中。

f 2/5/6 1/11/6 5/12/6 8/10/6 

现在,当我想将数据读入变量时,我该怎么做呢?这是我想要的格式。

stringstream s(line);
string tmpn;
int t[4];
int a, b, c, d, e;
s >>tmpn >>a >>t[0] >>b >>c >>t[1] >>b >>d >>t[2] >>b >>e >>t[3] >>b;

所以基本上我想要第一个字符,然后每个数字在没有斜杠的单独值中。

我该怎么做?我尝试使用 sscanf 但那太可怕了!我正在使用 C++/CLI。

最佳答案

如果您可以保证输入始终采用该格式,只需将斜杠替换为空格即可。

replace(line.begin(), line.end(), '/', ' ');

stringstream s(line);
string tmpn;
int t[4];
int a, b, c, d, e;
s >>tmpn >>a >>t[0] >>b >>c >>t[1] >>b >>d >>t[2] >>b >>e >>t[3] >>b;

(replace()<algorithm> header 中找到)

否则,您将不得不手动拆分它。

关于c++ - 如何格式化字符串流的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19413524/

62 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com