【经验】关于c++11中string类型字符串和整形相互转化的用法

https://blog.csdn.net/Elephant_King/article/details/129225134

 

c++11中为我们提供了许多非常方便的函数,可以帮助我们在整形与string类型字符串进行转换
关于Dev-c++如何使用c++11,因为本人是mac系统,使用cLion,无法安装Dev,可以在网上搜其他教程实现

整形转字符串(to_string())
to_string函数很好的帮助了我们进行从整形到字符串的转换

#include<bits/stdc++.h>
using namespace std;
int main(){
int a=123;
string s;

// 用法为
// string变量 = to_string(整形变量)
s= to_string(a);

cout<<s;
// 输出为123
long long b=123456789123;
// 长整形也可以实现
s= to_string(b);
cout<<s;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
字符串转整形(stoi)
#include<bits/stdc++.h>
using namespace std;
int main(){
string s="123";
int a;

// 用法为
// 整形变量=stoi(string类型变量)
a=stoi(s);
cout<<a;

// 同样长整形也能实现
long long b;
b= stoll(s);
cout<<b;
}
————————————————

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/Elephant_King/article/details/129225134

posted @ 2024-03-17 15:28  China Soft  阅读(21)  评论(0编辑  收藏  举报