25--------------------string转Int 用 stoi 函数,int 转string用 to_string 函数

本文介绍了如何使用C++标准库中的函数将字符串(string)转换为整数(int)及反之的方法。对于string转int,可以使用stoi函数;而对于int转string,则使用to_string函数。这些函数简单易用,能够帮助开发者快速实现类型转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、string 转 int 用stoi函数

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	string s="123";
	cout<<stoi(s)<<endl;
	 return 0;
}

2、int 转 string 用 to_string 函数

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	int s=123;
	cout<<to_string(s)<<endl;
	 return 0;
}