c++ 字符串反转 stl
将整数转换为字符串 (Converting integer to string)
It's often needed to convert integer datatype to string variable. C++ has its own library function to_string(), which converts any numerical value to its corresponding string type. For example, 123 converts to "123".
通常需要将整数数据类型转换为字符串变量 。 C ++有其自己的库函数to_string() ,该函数将任何数值转换为相应的字符串类型。 例如 , 123转换为“ 123” 。
First 123 is an integer, whereas "123" is string value
int i = 123;
string s = to_string(i);
前123是整数,而“ 123”是字符串值
整数i = 123;
字符串s = to_string(i);
to_string()函数详细信息 (to_string() Function details)
Prototype:
原型:
string to_string(int/long/long long);
Parameter: numerical value
参数:数值
Return type: string
返回类型:字符串
Example
例
Like we define and declare,
int i=5;
string s=to_string(i);
if(s=="5")
cout<
Remember, a string variable (literal) need to be defined under "". 'a' is a character whereas "a" is a string.
Header file needed:
C++ program to convert an integer to string
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cout<<"Input integer to convert\n";
cin>>n;
string s=to_string(n);
cout<<"Converted to string: "<<s<<endl;
return 0;
}
Output
Like we define and declare,
int i=5;
string s=to_string(i);
if(s=="5")
cout<
Remember, a string variable (literal) need to be defined under "" . 'a' is a character whereas "a" is a string.
Header file needed:
C++ program to convert an integer to string
# include < bits/stdc++.h >
using namespace std ;
int main ( ) {
int n ;
cout < < " Input integer to convert \n " ;
cin > > n ;
string s = to_string ( n ) ;
cout < < " Converted to string: " < < s < < endl ;
return 0 ;
}
Output
翻译自: https://www.includehelp.com/stl/convert-an-integer-to-string-in-cpp-stl.aspx
c++ 字符串反转 stl