C++ 如何初始化一个字符数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3134458/
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
how to initialize a char array?
提问by 5YrsLaterDBA
char * msg = new char[65546];
want to initialize to 0 for all of them. what is the best way to do this in C++?
想要将它们全部初始化为 0。在 C++ 中执行此操作的最佳方法是什么?
回答by Mike Seymour
char * msg = new char[65546]();
It's known as value-initialisation, and was introduced in C++03. If you happen to find yourself trapped in a previous decade, then you'll need to use std::fill()
(or memset()
if you want to pretend it's C).
它被称为值初始化,是在 C++03 中引入的。如果您碰巧发现自己陷入了前十年,那么您将需要使用std::fill()
(或者memset()
如果您想假装它是 C)。
Note that this won't work for any value other than zero. I think C++0x will offer a way to do that, but I'm a bit behind the times so I can't comment on that.
请注意,这不适用于零以外的任何值。我认为 C++0x 将提供一种方法来做到这一点,但我有点落后于时代,所以我不能对此发表评论。
UPDATE: it seems my ruminations on the past and future of the language aren't entirely accurate; see the comments for corrections.
更新:似乎我对语言过去和未来的反思并不完全准确;请参阅评论以进行更正。
回答by Peter Alexander
The "most C++" way to do this would be to use std::fill
.
执行此操作的“最 C++”方法是使用std::fill
.
std::fill(msg, msg + 65546, 0);
回答by Jerry Coffin
Absent a really good reason to do otherwise, I'd probably use:
如果没有一个很好的理由,我可能会使用:
std::vector<char> msg(65546, 'std::string msg(65546, 0); // all characters will be set to 0
');
回答by stinky472
what is the best way to do this in C++?
在 C++ 中执行此操作的最佳方法是什么?
Because you asked it this way:
因为你是这样问的:
std::vector<char> msg(65546); // all characters will be initialized to 0
Or:
或者:
some_c_function(&msg[0]);
If you are working with C functions which accept char* or const char*, then you can do:
如果您正在使用接受 char* 或 const char* 的 C 函数,那么您可以执行以下操作:
const uint size = 65546;
char* msg = new char[size];
memset(reinterpret_cast<void*>(msg), 0, size);
You can also use the c_str() method on std::string if it accepts const char* or data().
如果 c_str() 方法接受 const char* 或 data(),您也可以在 std::string 上使用它。
The benefit of this approach is that you can do everything you want to do with a dynamically allocating char buffer but more safely, flexibly, and sometimes even more efficiently (avoiding the need to recompute string length linearly, e.g.). Best of all, you don't have to free the memory allocated manually, as the destructor will do this for you.
这种方法的好处是你可以用动态分配的字符缓冲区做你想做的一切,但更安全、灵活,有时甚至更有效(避免线性重新计算字符串长度的需要,例如)。最重要的是,您不必释放手动分配的内存,因为析构函数会为您执行此操作。
回答by Steve Guidi
回答by Eric Petroelje
char * msg = new char[65546];
for(int i=0;i<65545;i++)
{
msg[i]='0';
}
msg[65545]='char msg[65536] = {0};
';
回答by Xpero
You can use a for loop. but don't forget the last char must be a null character !
您可以使用 for 循环。但不要忘记最后一个字符必须是空字符!
char msg[65536] = {'0' another 65535 of these separated by comma};
回答by Laurie Stearn
The C-like method may not be as attractive as the other solutions to this question, but added here for completeness:
类似 C 的方法可能不像这个问题的其他解决方案那么有吸引力,但为了完整起见,在此处添加:
You can initialise with NULLs like this:
您可以像这样使用 NULL 进行初始化:
msg[65536 - 1] = '##代码##'
Or to use zeros consider the following:
或者要使用零,请考虑以下事项:
##代码##But do not try it as not possible, so use memset!
但是不要尝试,因为不可能,所以使用memset!
In the second case, add the following after the memset if you want to use msgas a string.
在第二种情况下,如果要将msg用作字符串,请在 memset 后添加以下内容。
##代码##Answers to this questionalso provide further insight.
这个问题的答案也提供了进一步的见解。