本站使用了 Pjax 等基于 JavaScript 的开发技术,但您的浏览器已禁用 JavaScript,请开启 JavaScript 以保证网站正常显示!

c++整数转换为字符串

c++与c#不同,c#对面向对象的支持做的比较全面,在c#里面每一个变量都是一个对象,可以方便地调用ToString()方法将对象转换为字符串。但c++却没法这么方便地转换,需要利用一些方法转换。

(1)利用itoa函数

itoa函数原型

char *  itoa ( int value, char * str, int base );

使用指定的基数将整数值转换为以null终止的字符串,并将结果存储在str参数给定的数组中。

使用示例

/* itoa example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
  int i;
  char buffer [33];
  printf ("Enter a number: ");
  scanf ("%d",&i);
  itoa (i,buffer,10);
  printf ("decimal: %s\n",buffer);
  itoa (i,buffer,16);
  printf ("hexadecimal: %s\n",buffer);
  itoa (i,buffer,2);
  printf ("binary: %s\n",buffer);
  return 0;
}

或者

int a = 10;
char *intStr = itoa(a);
string str = string(intStr);

(2)使用stringstream

int a = 10;
stringstream ss;
ss << a;
string str = ss.str();

(3)使用to_string()函数

#include <string> 

std::string s = std::to_string(42);

或使用auto关键字

#include <string> 

auto s = std::to_string(42);

references

【1】C++ reference
【2】Stackoverflow


本文由芒果浩明发布,转载请注明出处。
本文链接:https://mangoroom.cn/cpp/some-ways-to-convert-int-to-string-in-cpp.html


 继续浏览关于 c++字符串 的文章

 本文最后更新于:2020/01/20 15:42:07,可能因经年累月而与现状有所差异

 引用转载请注明:芒果的博客 > c++ > c++整数转换为字符串