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;
}
字符串转整形(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;
}
C++11中的to_string与stoi函数使用教程
本文介绍了C++11引入的两个方便的函数,to_string用于将整形转换为字符串,而stoi则实现了字符串转整形。示例代码展示了在C++环境中,如何在main函数中使用这两个函数进行数据类型的转换,包括整型和长整型。此外,提到了在Mac系统下,可以使用CMake和CLion等工具来替代Dev-C++进行开发。
4785

被折叠的 条评论
为什么被折叠?



