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;
}
相关推荐
命运之光2 分钟前
qt中解决#include “main.moc“问题
开发语言·qt
CopyLower7 分钟前
Java 性能优化:从原理到实践的全面指南
java·开发语言·性能优化
七七知享15 分钟前
Python深度学习实现验证码识别全攻略
开发语言·python·深度学习·程序人生·程序员·开发·验证码
BC橡木31 分钟前
调度算法
c++
TOWNST1 小时前
Python Selenium 一小时速通教程
开发语言·python·selenium
努力努力再努力wz1 小时前
【Linux实践系列】:匿名管道收尾+完善shell外壳程序
linux·运维·服务器·c++
榴弹丶1 小时前
web通过离线编译protobuf,在线解析proto二进制数据
前端·c++·protobuf
楼田莉子2 小时前
C++学习记录:
开发语言·c++·学习