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;
}
相关推荐
CHEN5_021 小时前
【Java虚拟机】垃圾回收机制
java·开发语言·jvm
HalvmånEver2 小时前
在 C++ :x86(32 位)和 x64(64 位)的不同
开发语言·c++·学习
amy_jork4 小时前
npm删除包
开发语言·javascript·ecmascript
浪成电火花4 小时前
(deepseek!)deepspeed中C++关联部分
开发语言·c++
茉莉玫瑰花茶4 小时前
Qt 常用控件 - 9
开发语言·qt
艾伦~耶格尔5 小时前
【数据结构进阶】
java·开发语言·数据结构·学习·面试
杜子不疼.5 小时前
《Python列表和元组:从入门到花式操作指南》
开发语言·python
WYH2876 小时前
C#控制台输入(Read()、ReadKey()和ReadLine())
开发语言·c#
祈祷苍天赐我java之术6 小时前
Java 迭代器(Iterator)详解
java·开发语言
秋氘渔6 小时前
综合案例:Python 函数知识整合 — 学生成绩管理系统
开发语言·python