int与string类型转化(C++)

int转为string

  1. 利用sstream类
cpp 复制代码
#include <iostream>
#include <sstream>
using namespace std;

int main(){
    int n = 10;
    stringstream ss;
    string str;
    ss << n;
    ss >> str;
    cout << str << endl;
}
  1. sprintf
cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

int main(){
    int n = 10;
    char t[15];
    sprintf(t, "%d", n);  // 转成char类型
    cout << t << endl;
    string str(t);  //转成string类型
    cout << str << endl;

}
  1. to_string
    c++的版本的是C++11
cpp 复制代码
#include <iostream>
#include <string>
using namespace std;

int main(){
    int m = 10;
    string str;
    str = to_string(m);
    cout << str <<endl;
}
相关推荐
我叫袁小陌19 分钟前
C++多线程全面详解
开发语言·c++
lihongli00031 分钟前
【工程实战】Win11 + Ubuntu20.04 + Ubuntu24.04 三系统长期稳定安装方案(含避坑指南)
开发语言
黄宝康1 小时前
sublimetext 运行python程序
开发语言·python
m0_748250031 小时前
C++ 官方文档与标准
开发语言·c++
zh_xuan1 小时前
kotlin 类继承的语法2
开发语言·kotlin
matlabgoodboy1 小时前
程序代做python代编程matlab定制代码编写C++代写plc设计java帮做
c++·python·matlab
DYS_房东的猫2 小时前
《 C++ 零基础入门教程》第6章:模板与 STL 算法 —— 写一次,用万次
开发语言·c++·算法
诗意地回家2 小时前
淘宝小游戏反编译
开发语言·前端·javascript
wangkay882 小时前
【Java 转运营】Day04:抖音新号起号前准备全指南
java·开发语言·新媒体运营
点云SLAM2 小时前
C++ 静态初始化顺序问题(SIOF)和SLAM / ROS 工程实战问题
开发语言·c++·slam·静态初始化顺序问题·工程实战技术·c++static 关键字