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;
}
相关推荐
能工智人小辰3 分钟前
二刷 苍穹外卖day10(含bug修改)
java·开发语言
DKPT3 分钟前
Java设计模式之结构型模式(外观模式)介绍与说明
java·开发语言·笔记·学习·设计模式
筏.k13 分钟前
C++ 网络编程(14) asio多线程模型IOThreadPool
网络·c++·架构
LL.。27 分钟前
同步回调和异步回调
开发语言·前端·javascript
0wioiw039 分钟前
Python基础(吃洋葱小游戏)
开发语言·python·pygame
栗子~~1 小时前
Python实战- Milvus 向量库 使用相关方法demo
开发语言·python·milvus
狐凄1 小时前
Python实例题:基于 Flask 的在线聊天系统
开发语言·python
狐凄1 小时前
Python实例题:基于 Flask 的任务管理系统
开发语言·python
shootero@126.com1 小时前
R语言开发记录,一
开发语言·r语言
勤奋的知更鸟1 小时前
Java 编程之状态模式
java·开发语言·状态模式