C++ 这个代码有什么问题?我需要把字符串转换成浮点数
这个代码有什么问题?我需要把字符串转换成浮点数。m2在m2.length中有一个错误C++ 这个代码有什么问题?我需要把字符串转换成浮点数,c++,atof,type-conversion,C++,Atof,Type Conversion,这个代码有什么问题?我需要把字符串转换成浮点数。m2在m2.length中有一个错误 #包括 #包括 #包括 #包括 使用名称空间std; int main() { 浮动v2; char*m2=“1 23 45 6”; for(int i=0;i>v2){ 标准::cout 我需要转换数组中的每个元素,以便存储它们以进行操作 好的,那么使用字符串流将数字从字符串提取到向量中怎么样 #include <iostream> #include <iterator> #inclu
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
浮动v2;
char*m2=“1 23 45 6”;
for(int i=0;i
您的代码可能有太多错误,无法解释每一点。我会这样做:
float v2;
std::istringstream iss("1 23 45 6");
while(iss >> v2) {
std::cout << v2 << std::endl;
}
float v2;
标准:istringstream iss(“123456”);
而(iss>>v2){
标准::cout
我需要转换数组中的每个元素,以便存储它们以进行操作
好的,那么使用字符串流将数字从字符串提取到向量中怎么样
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
int main()
{
std::string input = "1 23 45 6";
std::stringstream ss(input);
std::istream_iterator<double> end;
std::vector<double> output(std::istream_iterator<double>(ss), end);
}
#包括
#包括
#包括
#包括
int main()
{
std::string input=“1 23 45 6”;
std::stringstream ss(输入);
std::istream_迭代器端;
std::矢量输出(std::istream_迭代器(ss),结束);
}
逐步分析,从错误中学习:
首先,字符数组没有成员函数,因此您应该定义string m2=“…”;
,以便编译代码
不幸的是,您的代码只会打印一个数字:最后一个。为什么?因为您的printf()
在循环之外
一旦你把它放入循环中,你会有很多数字,但比你预期的要多:
*第一次迭代将以“123456”=>1开始
*第二个为“23 45 6”=>23
*第三次迭代“23 45 6”=>23!
*第四次迭代“3 45 6”=>3(您是否预料到这一点?
因此,您必须从一个数字跳到下一个数字。因此,您可以使用函数搜索下一个空格,而不是递增:
for (int i = 0; i < m2.length(); i=m2.find_first_of(' ', i+1)) // jump to the next space
{
v2 = atof(&m2[i]);
printf("%.2f\n", v2);
}
for(int i=0;i
给你
<强>实数C++替代:< /强>
看一下πνταῥεῖ强>的解决方案:这是我们在C++中使用的方法:<代码>使用字符串<代码> >代替<代码> char */COD>,使事情变得更容易。
要将给定字符串转换为数值,请使用stringstream
或atof()
快速解决您的问题:
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
vector<float> myFloats;
string m("1. 2.23 45 6456 72.8 46..");
// First of parse given string and separate char groups that serve as a number
// => only consider 0-9 and '.'
if(m.size() == 0)
return 1;
string charGroup;
for(int i=0; i<m.length(); i++)
{
if((isdigit(m.at(i)) || m.at(i) == '.'))
{
charGroup += m.at(i);
if(i == m.length() - 1 && !charGroup.empty())
{
// in case given group is a numerical value
myFloats.push_back((float) atof(charGroup.c_str()));
// prepare for next group
charGroup.clear();
}
}
else if(!charGroup.empty())
{
if(m.at(i) == ' ')
{
// in case given group is a numerical value
myFloats.push_back((float) atof(charGroup.c_str()));
// prepare for next group
charGroup.clear();
}
else charGroup.clear();
}
}
// Print float values here
if(!myFloats.empty())
{
cout << "Floats: ";
for(int i=0; i<myFloats.size(); i++)
cout << myFloats.at(i) << ", ";
}
getchar();
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
向量myfloat;
字符串m(“1.2.2345645672.846..”);
//首先解析给定字符串和用作数字的单独字符组
//= >只考虑0—9和‘0’。
如果(m.size()==0)
返回1;
字符串字符组;
对于(int i=0;i“此代码有什么问题?”-Le'ts请参见…使用命名空间std
,atof
,printf
,system
,以及m2.length()
字符*
没有提供length()
成员函数。您可能打算改用std::string
,以及size()
成员函数。您真正想要实现的是什么?在循环内重复分配给v2
,但在循环完成后只打印一次v2
,这听起来不是很有用。如果您想将每个元素转换为浮点,一个浮点将不足以保存结果。请使用std::vector values;
来存储浮点数。@EzzEddinAbdullah您需要包含这样的信息,这会完全改变您的问题。您不是在尝试将字符串转换为浮点数,而是在尝试将空格分隔的字符串转换为许多浮点数。我想这两个答案都不能解决这个问题。那么,如何打印向量?您必须迭代er并打印每个元素。使用for循环。这是什么意思:while(iss>>v2)@EzzedInAbdullah它解析下一个数字的输入,并将其分配给v2
,将跳过空白。如果解析失败或不再存在数据,则streams状态将为false,循环结束。此函数具体做什么?它是否将字符串转换为数组?或仅将解析输入的一个元素转换为数组?。
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
vector<float> myFloats;
string m("1. 2.23 45 6456 72.8 46..");
// First of parse given string and separate char groups that serve as a number
// => only consider 0-9 and '.'
if(m.size() == 0)
return 1;
string charGroup;
for(int i=0; i<m.length(); i++)
{
if((isdigit(m.at(i)) || m.at(i) == '.'))
{
charGroup += m.at(i);
if(i == m.length() - 1 && !charGroup.empty())
{
// in case given group is a numerical value
myFloats.push_back((float) atof(charGroup.c_str()));
// prepare for next group
charGroup.clear();
}
}
else if(!charGroup.empty())
{
if(m.at(i) == ' ')
{
// in case given group is a numerical value
myFloats.push_back((float) atof(charGroup.c_str()));
// prepare for next group
charGroup.clear();
}
else charGroup.clear();
}
}
// Print float values here
if(!myFloats.empty())
{
cout << "Floats: ";
for(int i=0; i<myFloats.size(); i++)
cout << myFloats.at(i) << ", ";
}
getchar();
return 0;
}