visual-studio _itoa 和 itoa 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1586749/
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
What is the difference between _itoa and itoa?
提问by Polaris878
Visual Studio is yelling at me about using itoa()saying to use _itoa()instead?
Visual Studio 对我大喊大叫,itoa()说要使用_itoa()?
It looks to me like they are the same function. What gives?
在我看来,它们的功能相同。是什么赋予了?
回答by Michael Burr
A C run time library implementation is not supposed to introduce names that aren't in the standard unless they follow a certain naming convention (like starting with an underscore). The earlier versions of Microsoft's compiler didn't follow this rule particularly closely, but over time, Microsoft has been moving more toward making their implementation more standards compliant. So functions they used to supply that would intrude on the user's namespace they have been implementing using names that are reserved for compiler implementations and have been deprecating the old names.
AC 运行时库实现不应该引入标准中没有的名称,除非它们遵循特定的命名约定(例如以下划线开头)。早期版本的 Microsoft 编译器并没有特别严格地遵循此规则,但随着时间的推移,Microsoft 一直在朝着使其实现更符合标准的方向发展。因此,他们过去提供的函数会侵入用户的命名空间,他们一直在使用为编译器实现保留的名称来实现,并且一直在弃用旧名称。
If _CRT_NONSTDC_NO_WARNINGSis defined, the MS compiler won't complain about the itoa()function being deprecated. But it will still complain about it being unsafe (you have to define _CRT_SECURE_NO_WARNINGSto quiet that warning). Or use the safer version of the function (_itoa_s()) that provides the function with the destination buffer size
如果_CRT_NONSTDC_NO_WARNINGS已定义,MS 编译器将不会抱怨该itoa()函数被弃用。但是它仍然会抱怨它不安全(您必须定义_CRT_SECURE_NO_WARNINGS以消除该警告)。或者使用为函数_itoa_s()提供目标缓冲区大小的函数 ( )的更安全版本
Both _itoa()and itoa()resolve to the exact same function in the library down to the same address - there is no difference except in the name.
双方_itoa()并itoa()决心在库到相同的地址完全一样的功能-有除了在这个名字没有什么区别。
回答by Greg Hewgill
The MSDN documentation for itoa()says:
This POSIX function is deprecated beginning in Visual C++ 2005. Use the ISO C++ conformant
_itoaor security-enhanced_itoa_sinstead.
从 Visual C++ 2005 开始不推荐使用此 POSIX 函数。请改用符合 ISO C++
_itoa或安全性增强的函数_itoa_s。
回答by Robert Massaioli
itoa is not standard C.
itoa 不是标准的 C.
"This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers." - cplusplus.com
“这个函数没有在 ANSI-C 中定义,也不是 C++ 的一部分,但被一些编译器支持。” - cplusplus.com
So MSVS is telling you to use the _itoa to tell you that it is not standard C++ and that you should mark it as such. I believe that it is there for backwards compatibility and that this notation is for readability and distinction.
所以 MSVS 告诉你使用 _itoa 来告诉你它不是标准的 C++,你应该这样标记它。我相信它是为了向后兼容而存在的,这个符号是为了可读性和区分。
回答by Corey
itoais not standard, so you should use stringstream instead.
itoa不是标准的,所以你应该使用 stringstream 代替。
you'll need #include <sstream>
你需要 #include <sstream>
an example of it's use would be:
它的使用示例是:
int i = 5;
std::stringstream ss;
ss << i;
std:: cout << ss.str();
回答by kotarou3
In reply to the answer by Bruce:
回复布鲁斯的回答:
itoais not standard, so you should use stringstream instead.you'll need
#include <sstream>an example of it's use would be:
int i = 5;std::stringstream ss;
ss << i;
std:: cout << ss.str();
itoa不是标准的,所以你应该使用 stringstream 代替。你需要
#include <sstream>它的使用示例是:
int i = 5;std::stringstream ss;
ss << i;
std:: cout << ss.str();
You can also code your own itoa()function instead
您也可以编写自己的itoa()函数
eg:
例如:
const char* itoa (int num)
{
if (num == 0)
{
return "0";
}
bool neg = false;
if (num < 0)
{
neg = true;
num = -num;
}
int digits = 0;
int tmp = num;
while (tmp > 0)
{
digits++;
tmp /= 10;
}
int digs[digits];
for (tmp = digits; num > 0; tmp--)
{
digs[tmp] = num % 10;
num /= 10;
}
string s = neg == true ? "-" : "";
for (tmp = 1; tmp <= digits; tmp++)
{
s += (char)(digs[tmp] + 48);
}
return s.c_str();
}

