栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ string与int的转换

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ string与int的转换

怎么将string转换为int

方法一、使用自带的stoi()函数

#include 
#include 
using namespace std;

int main()
{
	string str="123";
	int intStr=stoi(str);
	cout< 

结果:

$g++ -o main *.cpp
$main
123

当然你也可以使用一些别的方法,这里我只是介绍简单的方法。
除了可以将string转换为int,还可以用自带的stol(),stoll()等自带的方法转换为long 和long long格式。

方法二、使用自带的atoi()函数

#include 
#include 
using namespace std;

int main()
{
	string s = "12"; 
	int a = atoi(s.c_str());
	cout< 

怎么将int转换为string

使用自带的to_string函数

#include 
#include 
using namespace std;

int main()
{
	double pi = 3.1415;
	string s = to_string(pi);
	cout< 

结果:

$g++ -o main *.cpp
$main
3.141500
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/784506.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号