C ++如何将整数更改为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10220885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:45:56  来源:igfitidea点击:

C++ How do you change an integer to a string?

c++visual-c++

提问by Boom_mooB

Possible Duplicate:
Alternative to itoa() for converting integer to string C++?

可能的重复:
替代 itoa() 将整数转换为字符串 C++?

How do you change an integer to a string in c++?

在 C++ 中,如何将整数更改为字符串?

采纳答案by Asik

Standard C++ library style:

标准 C++ 库风格:

#include <sstream>
#include <string>

(...)

int number = 5;
std::stringstream ss;
ss << number;
std::string numberAsString(ss.str());

Or if you're lucky enough to be using C++11:

或者,如果您有幸使用 C++11:

#include <string>

(...)

int number = 5;
std::string numberAsString = std::to_string(number);

回答by DavidChuBuaa

You could use snprintf(char *str, size_t size, const char *format, ...)to get a char[], then use string(char*)get string. Of course,there're other ways.

您可以使用snprintf(char *str, size_t size, const char *format, ...)获取字符 [],然后使用string(char*)获取字符串。当然,还有其他方法。