C++ to_string()函数

系列文章目录


文章目录

  • 系列文章目录
  • 前言
  • 一、C++ to_string()函数详解
  • 二、example
  • 总结


前言

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:

C++ to_string()函数_第1张图片
Return Value

A string object containing the representation of val as a sequence of
characters.

二、example

代码如下:

// to_string example
#include    // std::cout
#include      // 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

你可能感兴趣的:(C++,c++)