一般来说,或者更具体地说,在竞争性编程中,有很多情况下,我们需要将数字转换为字符串,或者将字符串转换为数字。但由于缺乏对某些基本工具的了解,我们不得不这么做。本文介绍了实现这一任务的一些方法。
将字符串转换为数字
方法1:使用stringstream类或sscanf(): stringstream是一种操作字符串的方便方法。sscanf()是一个类似于scanf()的C风格函数。它从字符串而不是标准输入中读取输入。
方法2:使用stoi()或atoi()进行字符串转换。
- stoi():stoi()函数将字符串作为参数并返回其值。
- atoi():atoi()函数将字符数组或字符串文字作为参数,并返回其值。
这两种方法已在中详细讨论 这 文章
方法3:使用boost词法转换: Boost库提供了一个内置函数“lexical_cast(“string”)”,它直接将字符串转换为数字。如果输入无效,它会返回一个异常“bad_lexical_cast”。
CPP
// C++ code to demonstrate working of lexical_cast() #include <boost/lexical_cast.hpp> // for lexical_cast() #include <iostream> #include <string> // for string using namespace std; int main() { string str = "5" ; string str1 = "6.5" ; // Initializing f_value with casted float // f_value is 6.5 float f_value = boost::lexical_cast< float >(str1); // Initializing i_value with casted int // i_value is 5 int i_value = boost::lexical_cast< int >(str); // Displaying casted values cout << "The float value after casting is : " ; cout << f_value << endl; cout << "The int value after casting is : " ; cout << i_value << endl; return 0; } |
The float value after casting is : 6.5The int value after casting is : 5
将数字转换为字符串
方法1:使用字符串流: 在这个方法中,字符串流声明一个流对象,它首先将一个数字作为流插入一个对象,然后使用“str()”来完成数字到字符串的内部转换。
实施:
CPP
// C++ code to demonstrate string stream method // to convert number to string. #include<iostream> #include <sstream> // for string streams #include <string> // for string using namespace std; int main() { int num = 2016; // declaring output string stream ostringstream str1; // Sending a number as a stream into output // string str1 << num; // the str() converts number into string string geek = str1.str(); // Displaying the string cout << "The newly formed string from number is : " ; cout << geek << endl; return 0; } |
The newly formed string from number is : 2016
方法2:使用to_string() :此函数接受一个数字(可以是任何数据类型),并返回所需字符串中的数字。
CPP
// C++ code to demonstrate "to_string()" method // to convert number to string. #include <iostream> #include <string> // for string and to_string() using namespace std; // Driver Code int main() { // Declaring integer int i_val = 20; // Declaring float float f_val = 30.50; // Conversion of int into string using // to_string() string stri = to_string(i_val); // Conversion of float into string using // to_string() string strf = to_string(f_val); // Displaying the converted strings cout << "The integer in string is : " ; cout << stri << endl; cout << "The float in string is : " ; cout << strf << endl; return 0; } |
The integer in string is : 20The float in string is : 30.500000
方法3:使用boost词法转换: 与字符串转换类似,“lexical_cast()”函数保持不变,但这次参数列表修改为“lexical_cast(numeric_var)”。
CPP
// C++ code to demonstrate "lexical_cast()" method // to convert number to string. #include <boost/lexical_cast.hpp> // for lexical_cast() #include <iostream> #include <string> // for string using namespace std; // Driver Code int main() { // Declaring float float f_val = 10.5; // Declaring int int i_val = 17; // lexical_cast() converts a float into string string strf = boost::lexical_cast<string>(f_val); // lexical_cast() converts a int into string string stri = boost::lexical_cast<string>(i_val); // Displaying string converted numbers cout << "The float value in string is : " ; cout << strf << endl; cout << "The int value in string is : " ; cout << stri << endl; return 0; } |
The float value in string is : 10.5The int value in string is : 17
本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。