C++ --- 字符串与数字的转换

数字转string

std::to_string(int)

std::to_string(long)

std::to_string(long long)

std::to_string(float)

std::to_string(double)

std::to_string(long double)

1 #include <string>
2 #include <iosream>
3 using namespace std;
4 int main()
5 {
6     int num=123;
7     string str=to_string(num);
8     cout<<str<<endl;
9    return 0;

string转数字

头文件:cstdlib

std::stoi

std::stol

std::stoll

#include<cstdlib.h>
using namespace std;
int main()
{
    int num=0;
    string str="123";
    num=stoi(str);
    cout<<num<<endl;
   return 0;

其他:

提取字符串中的指定位置(截取字符串、提取字符串)

原文地址:https://www.cnblogs.com/y4247464/p/14073141.html