c++ 中使用静态类型。为了编写程序,变量必须定义为特定类型。有时必须读取来自控制台或文件的输入。在这种情况下,程序被赋予字符串数据。需要特殊操作才能将它们转换为其他数据类型。本文将提供将字符串转换为浮点整数的 c++ 方法。可以使用几种不同的方法来实现此目的。分别探索它们中的每一个。
在 c++ 中使用字符串流流是c++中的一个很棒的工具。文件流、标准输入/输出流等都是这些流的例子。stringstream是一个不同的流存在。它通过接受一个字符串作为输入来进行操作,类似于其他流。我们必须导入sstream头文件才能使用stringstream。可以使用插入运算符(>>)或提取运算符(<<)来获取流数据。
语法#include < sstream >stringstream streamobject ( <a string input> );
要使用流读取特定类型的输入,语法如下 -
语法<data type> variable;streamobject >> variable;
算法让我们看看算法以了解其整体工作原理。
将字符串对象x作为输入<li>创建一个stringstream对象,命名为ss,并将x传递给该对象</li>创建一个浮点变量 xfloat使用 ss 中的插入运算符将浮点数存储到 xfloat示例#include <iostream>#include <sstream>using namespace std;float solve( string mystring) { float x; stringstream ss( mystring ); ss >> x; return x;}int main(){ string anumber = 3.14159; float convnumber = solve( anumber ); cout << the given number is: << convnumber << endl; cout << 6.5 more than the given number is: << convnumber + 6.5 << endl;}
输出the given number is: 3.141596.5 more than the given number is: 9.64159
从这个示例中可以明显看出,该数字是从字符串对象中检索的。因为这是实际的浮点数据,所以我们可以用浮点表示法将 6.5 添加到自身并显示结果。
在c++中使用sscanf()一种可比较的方法(在c中也适用)是使用sscanf()函数。该函数接受一个字符数组作为输入和格式字符串,就像标准的scanf()函数一样。现在它从字符串中读取所请求的值,并将其附加到由变量的地址指向的变量上。请查看sscanf()函数的语法。
语法scanf ( <a string input>, <format string>, address(s) of variable );
算法让我们看看算法以了解其整体工作原理。
将字符串 x 作为类似 c 字符数组格式的输入使用带参数 x、格式字符串和变量地址的 sscanf() 函数变量值将直接从sscanf()函数中存储。示例#include <iostream>#include <sstream>using namespace std;float solve( string mystring) { float x; sscanf( mystring.c_str(), %f, &x ); return x;}int main(){ string anumber = 6.8; float convnumber = solve( anumber ); cout << the given number is: << convnumber << endl; cout << 2.5 more than the given number is: << convnumber + 2.5 << endl;}
输出the given number is: 6.82.5 more than the given number is: 9.3
应用程序的运行方式与以前完全相同,但有一些我们必须注意的地方。 sscanf() 方法不支持类似 c++ 的字符串对象。它需要一个类似于 c 的字符数组。为了实现这一点,我们使用 c_str() 方法将提供的字符串参数转换为类似于 c 的字符数组。
在c++中使用stof()使用“字符串”头文件中的 stof() 方法是将字符串转换为整数的另一种快速而简单的方法。该函数在接收到字符串对象作为输入后将其转换为相应的浮点数。
语法#include <string>stof ( <integer in string format> );
算法将字符串对象x作为输入xfloat = stoi( x )从给定字符串 x 返回 xfloat 作为浮动变量。示例#include <iostream>#include <sstream>using namespace std;float solve( string mystring) { float x; x = stof( mystring ); return x;}int main(){ string anumber = 6.8; float convnumber = solve( anumber ); cout << the given number is: << convnumber << endl; cout << 2.5 more than the given number is: << convnumber + 2.5 << endl;}
输出the given number is: 6.82.5 more than the given number is: 9.3
在c++中使用atof()尽管 atof() 在 c 语言中也可用,但它与 stof 相当。可以使用字符数组格式提交字符串。通过导入cstdlib库,你就可以得到它。否则,就没有真正的区别。让我们检查一下语法。
语法#include <cstdlib>atof ( <floating number in character array format> );
算法将字符串对象x作为输入,以c语言字符数组格式。xfloat = atoi( x )从给定字符串 x 返回 xfloat 作为浮点变量。示例#include <iostream>#include <sstream>using namespace std;float solve( string mystring) { float x; x = atof( mystring.c_str() ); return x;}int main(){ string anumber = 8.9; float convnumber = solve( anumber ); cout << the given number is: << convnumber << endl; cout << 6.5 more than the given number is: << convnumber + 6.5 << endl;}
输出the given number is: 8.96.5 more than the given number is: 15.4
结论有多种方法可以将字符串转换为浮点数。前两种方法(使用 stringstream 和 sscanf())是将字符串转换为任何数据类型而无需更改任何其他内容的通用方法;唯一会改变的是最终变量的类型。 stof() 和 atof() 这些函数仅用于将字符串转换为浮点数。转换为不同数据类型的其他函数是等效的。由于它们是基于 c 的函数,因此 sscanf 和 atof() 不接受字符串对象。在使用它们之前,我们必须使用 c_str() 函数将字符串转换为字符数组。
以上就是将以下内容翻译为中文:c++程序将字符串转换为浮点数的详细内容。