简体   繁体   English

在C ++中使用itoa()函数将整数转换为字符串的时间复杂度?

[英]Time complexity to convert a integer to string using itoa() function in c++?

I want to analyze the complexity of my code and when i saw itoa() function call then i thought is it constant time or is it converted to string by doing % and / operators.(i want to convert integer to its binary string). 我想分析代码的复杂性,当我看到itoa()函数调用时,我认为它是恒定时间还是通过执行%和/运算符将其转换为字符串。(我想将整数转换为其二进制字符串)。

  int i;
  char buffer [33];
  printf("Enter a number: ");
  scanf ("%d",&i);
  itoa (i,buffer,10);  //1st
  printf ("decimal: %s\n",buffer);
  itoa (i,buffer,16);  //2nd
  printf ("hexadecimal: %s\n",buffer);
  itoa (i,buffer,2);  //3rd
  printf ("binary: %s\n",buffer);

The source code can be found here: 源代码可以在这里找到:

https://en.wikibooks.org/wiki/C_Programming/stdlib.h/itoa https://zh.wikibooks.org/wiki/C_Programming/stdlib.h/itoa

It seems like it's doing the standard mod div approach. 似乎它正在执行标准的mod div方法。 Also, one of the external links provides some relative performance benchmarks at the bottom of the page: 此外,外部链接之一在页面底部提供了一些相对性能基准:

http://www.strudel.org.uk/itoa/ http://www.strudel.org.uk/itoa/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM