系列文章目录
前言
http://www.cplusplus.com/reference/string/to_string/
一、C++ to_string()函数详解
std::to_string
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
Convert numerical value to string
Returns a string with the representation of val.
The format used is the same that printf would print for the corresponding type:
Return Value
A string object containing the representation of val as a sequence of
characters.
二、example
代码如下:
// to_string example
#include <iostream> // std::cout
#include <string> // std::string, std::to_string
int main ()
{
std::string pi = "pi is " + std::to_string(3.1415926);
std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";
std::cout << pi << '\n';
std::cout << perfect << '\n';
return 0;
}
OUTPUT:
pi is 3.141593
28 is a perfect number
总结
以上就是今天要讲的内容
本文作者:WeSiGJ
欢迎各位兄弟姐妹们,加入C++ HTTP服务器开发技术交流群:
QQ群:426685924
![](https://i-blog.csdnimg.cn/blog_migrate/ed0fb08666709db85facc885c274f878.png)