c++ 字符串反转 stl_如何在C ++ STL中将整数转换为字符串?

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.


    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





评论和讨论

广告:您是博主吗? 加入我们的Blogging论坛


翻译自: https://www.includehelp.com/stl/convert-an-integer-to-string-in-cpp-stl.aspx

c++ 字符串反转 stl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值