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 分钟前
QT_QFontDialog类字体对话框
开发语言·qt
圣光SG7 分钟前
Java操作题练习(七)
java·开发语言·算法
麻瓜老宋1 小时前
AI开发C语言应用按步走,表达式计算器calc的第二十三步,多行输入、进制输出、错误恢复、常量折叠、配置加载等
c语言·开发语言·atomcode
q567315232 小时前
企业级 HTTP 代理采购选型:技术评估清单 15 项
开发语言·网络·爬虫·网络协议·http·隧道ip·代理ip
天天进步20152 小时前
Python全栈项目--智能办公自动化系统
开发语言·python
拳里剑气3 小时前
C++算法:多源BFS
c++·算法·宽度优先·多源bfs
cm04Z9c913 小时前
C#摸鱼实录——IoC与DI案例详解
开发语言·c#
ysa0510303 小时前
【板子】短序列dp(换成维护更小常数维度的dp)
c++·笔记·算法·板子
long3163 小时前
Java 新手入门与实战开发指南
java·开发语言
摩西蒙4 小时前
计算机网络
开发语言·计算机网络·php