将字符串转换为数字有两种常用方法:
使用stringstream类或sscanf() stringstream(): 这是一种将数字字符串转换为整数、浮点或双精度的简单方法。下面是一个使用stringstream将字符串转换为int的示例程序。
CPP
// A program to demonstrate the use of stringstream #include <iostream> #include <sstream> using namespace std; int main() { string s = "12345" ; // object from the class stringstream stringstream geek(s); // The object has the value 12345 and stream // it to the integer x int x = 0; geek >> x; // Now the variable x holds the value 12345 cout << "Value of x : " << x; return 0; } |
输出:
Value of x : 12345
// A stringstream is similar to input/output// file stream. We need to declare a stringstream// just like an fstream, for example: stringstream ss;// and, like an fstream or cout, // we can write to it:ss << myString; or ss << myCstring; orss << myInt;, or float, or double, etc.// and we can read from it:ss >> myChar; orss >> myCstring; orss >> myInt;
总之,stringstream是一种操作字符串的方便方法。 sscanf() 是一个类似于scanf()的C风格函数。它从字符串而不是标准输入中读取输入。
CPP
#include<stdio.h> int main() { const char *str = "12345" ; int x; sscanf (str, "%d" , &x); printf ( "The value of x : %d" , x); return 0; } |
输出:
Value of x : 12345
同样,我们可以分别使用%f和%lf读取float和double。
使用stoi()或atoi()进行字符串转换 stoi(): 函数的作用是:将字符串作为参数,并返回其值。以下是一个简单的实现:
CPP
// C++ program to demonstrate working of stoi() // Work only if compiler supports C++11 or above. #include <iostream> #include <string> using namespace std; int main() { string str1 = "45" ; string str2 = "3.14159" ; string str3 = "31337 geek" ; int myint1 = stoi(str1); int myint2 = stoi(str2); int myint3 = stoi(str3); cout << "stoi("" << str1 << "") is " << myint1 << '' ; cout << "stoi("" << str2 << "") is " << myint2 << '' ; cout << "stoi("" << str3 << "") is " << myint3 << '' ; return 0; } |
输出:
stoi("45") is 45stoi("3.14159") is 3stoi("31337 geek") is 31337
atoi(): 函数的作用是:将字符数组或字符串文字作为参数,并返回其值。以下是一个简单的实现:
CPP
// For C++11 above #include <cstdlib> #include <iostream> using namespace std; int main() { const char * str1 = "42" ; const char * str2 = "3.14159" ; const char * str3 = "31337 geek" ; int num1 = atoi (str1); int num2 = atoi (str2); int num3 = atoi (str3); cout << "atoi("" << str1 << "") is " << num1 << '' ; cout << "atoi("" << str2 << "") is " << num2 << '' ; cout << "atoi("" << str3 << "") is " << num3 << '' ; return 0; } |
C
#include <stdio.h> #include <stdlib.h> int main() { char * str1 = "42" ; char * str2 = "3.14159" ; char * str3 = "31337 geek" ; int myint1 = atoi (str1); int myint2 = atoi (str2); int myint3 = atoi (str3); printf ( "stoi(%s) is %d " , str1, myint1); printf ( "stoi(%s) is %d " , str2, myint2); printf ( "stoi(%s) is %d " , str3, myint3); // This Code is Contributed by Harshit Srivastava return 0; } |
输出:
atoi("42") is 42atoi("3.14159") is 3atoi("31337 geek") is 31337
stoi()与atoi()之比较
- atoi()是一个传统的C风格函数。在C++ 11中添加了SUTIE()。
- ATOI-()只用于C样式字符串(字符数组和字符串文字),StII()用于C++字符串和C样式字符串
- atoi()只接受一个参数并返回整数值。
int atoi (const char * str);
- stoi()最多可以包含三个参数,第二个参数用于起始索引,第三个参数用于输入编号的基数。
int stoi (const string& str, size_t* index = 0, int base = 10);
运动 编写自己的atof(),它将字符串(表示浮点值)作为参数,并以double返回其值。 参考: http://www.cplusplus.com/reference/string/stoi/ http://www.cplusplus.com/reference/sstream/stringstream/ http://www.cplusplus.com/reference/cstdlib/atoi/ 本文由 西菲·辛格 .如果你喜欢GeekSforgek,并且想贡献自己的力量,你也可以写一篇文章,并将文章邮寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。