如何从字符串中提取浮子?
我试图从字符串中提取浮点数,然后将它们保存在数组中。
这是我发现的代码,即使我进行了必要的更改,但它行不通:
#include <iostream>
#include <string>
using namespace std;
string a="2,134 43,54 22,334";
string b[30];
int found,i=0;
while(a!="\0"){
found=a,find("\t");
for(int f=0;f<found;f++){
b[i]+=a[f];
}
a.erase(0,found+1);
i++;
}
for(int d=0;d<i;d++){
cout<<b[d]<<endl;
}
return 0;
}
I am trying to extract float numbers from a string and after that they will be saved in an array.
Here's a code I found and even though I made the necessary changes it doesn't work:
#include <iostream>
#include <string>
using namespace std;
string a="2,134 43,54 22,334";
string b[30];
int found,i=0;
while(a!="\0"){
found=a,find("\t");
for(int f=0;f<found;f++){
b[i]+=a[f];
}
a.erase(0,found+1);
i++;
}
for(int d=0;d<i;d++){
cout<<b[d]<<endl;
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要这样解析字符串。只需阅读所需语言环境中的值,例如大多数非英语欧洲地区,例如
de_de.utf8
,ru_ru.utf8
,或it_it.utf-8
...您甚至可以为不同的流设置不同的位置,因此,下面我使用std :: cin
和自定义的默认系统语言环境使用:
作为std :: Cout
中的radix点std :: locale(“”)
给您当前系统locale ,它可能是el_gr.utf -8
在您的情况下。您还可以指定使用的特定语言环境,例如std :: locale(“ fr_fr.utf8”)
。然后使用std :: Locale :: locale :: global
在全球范围内设置获得的区域。每个特定的流都可以进一步imbue
d 在必要时到其他地方。您也可以使用setlocale
用于设置一些位置首选项样本输出:
注意最后输出的差异?那是因为
,
是上面的英语区域中的千分离器,我是通过
lc_all
,但是在Windows上,您无法轻松地从控制台更改它,因此只需在代码中执行此操作即可。 And I'm printing the output directly but pushing it into an array is trivial请注意,这些在线平台没有非US的网站元素,因此我必须使用自定义语言环境。在Linux上,您可以使用
locale -A
检查可用的地区,以防您真的想将浮点数作为字符串作为字符串(为什么?),然后只是正常阅读。无需进行这种复杂的解析。
std :: cin
和任何种类的istream /code>
只会停在空格 expected
Sample output:
Demo
Don't parse the string like that. Just read the values in your desired locale, for example most non-English European locales like
de_DE.utf8
,ru_RU.utf8
, orit_IT.UTF-8
... You can even set different locales for different streams, so for example below I'm using the default system locale forstd::cin
and a custom locale which uses:
as the radix point instd::cout
In C++
std::locale("")
gives you the current system locale which is probablyel_GR.UTF-8
in your case. You can also specify a specific locale to use such asstd::locale("fr_FR.utf8")
. Then usestd::locale::global
to set the obtained locale globally. Each specific stream can further beimbue
d to a different locale if necessary. You can also usesetlocale
for setting some locale preferencesSample output:
Notice the difference in the last output? That's because
,
is the thousand separator in the English localeIn the example above I'm setting the current locale via
LC_ALL
, but on Windows you can't change that easily from the console so just do that in your code. And I'm printing the output directly but pushing it into an array is trivialNote that those online platforms don't have a non-US locale so I have to use the custom locale. On Linux you can check the available locales with
locale -a
In case you really want to get the floating-point numbers as strings (why?) then just read normally. No need for such complex parsing.
std::cin
and any kinds ofistream
will just stop at blank spaces as expectedSample output:
Demo
如果您不太关心性能,则可以使用此简单算法:
此算法会通过“ A”中的每个字符进行检查:
如果您想我解释一些有时可以提出要求:)
If you don't care about performance that much, you can use this simple algorithm :
this algorithm goes through every char inside 'a' and checks:
if you want me to explain someting feel free to ask :)