C++ 如何将整个字符数组设置为空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28397936/
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
C++ How do I set an entire character array to blank spaces?
提问by tomkelley13
Say I have the following declared variable:
假设我有以下声明的变量:
char mychararray[35];
and I want to set every character in the array to a blank space... How can I do that?
我想将数组中的每个字符设置为一个空格......我该怎么做?
My instructor told me all I had to do was put
我的导师告诉我我所要做的就是把
mychararray = "";
but that didn't work at all...
但这根本不起作用......
Am I using an outdated version of Visual Studio (2012), or is this just a bad initialization? If it is the latter, please explain how to make all the characters a blank space.
我使用的是过时版本的 Visual Studio (2012),还是这只是一个错误的初始化?如果是后者,请说明如何使所有字符都为空格。
Thanks in advance!
提前致谢!
回答by Ishamael
You can initialize it the way your instructor suggested as you declare the array:
您可以在声明数组时按照教师建议的方式对其进行初始化:
char mychararray[35] = "";
It will set the array to an empty string.
它会将数组设置为空字符串。
If you want to make it an empty string later, then you can just do
如果你想稍后使它成为一个空字符串,那么你可以这样做
mychararray[0] = 'memset(mychararray, ' ', 34);
mychararray[34] = 'char mychararray[35] = "";
';
';
If you want to make it an array consisting of 34 spaces (35th character being null terminator), then
如果要使其成为由 34 个空格(第 35 个字符为空终止符)组成的数组,则
#include <algorithm>
#include <iterator>
int main()
{
char c[35] = "Hello, world!";
std::fill(std::begin(c), std::end(c), 'std::string blanks(35, ' ');
');
// c will contain only zeros now.
}
回答by Andy Prowl
That is not initialization, it's assignment. For initializing an array when declaring it, you can write:
那不是初始化,而是赋值。要在声明数组时初始化数组,您可以编写:
memset(mychararray, ' ', 34);
mychararray[34]='char mychararray[35] = {};
';
If you already have an array and want to set it to zero, you can use std::fill
:
如果您已经有一个数组并希望将其设置为零,则可以使用std::fill
:
However, C-style arrays are not very popular in modern C++. Prefer using std::string
(which you can clear using the clear()
member function), or std::array<char, N>
if you need the size to be known at compile-time.
然而,C 风格的数组在现代 C++ 中并不是很流行。更喜欢使用std::string
(您可以使用clear()
成员函数清除它),或者std::array<char, N>
如果您需要在编译时知道大小。
回答by xtofl
Although you could use std::fill_n(array, 35, ' ')
, you're probably better of using an std::string
, the preferred string class in C++. Don't have to care about zero-termination or anything.
尽管您可以使用std::fill_n(array, 35, ' ')
,但最好使用std::string
C++ 中首选的字符串类 。不必关心零终止或任何事情。
回答by Pradhan
std::memset
is your friend.
std::memset
是你的朋友。
If you want to initialize the array with all elements being '\0'
, you have a neater way through aggregate initialization:
如果要使用所有元素初始化数组'\0'
,则可以通过聚合初始化更简洁的方法:
回答by Marcus Müller
C++ doesn't initialie char
to ' '
(which has the ascii value 0x20, btw); in fact, array initialization without specifying what to use for initialization doesn't necessarily take place (arrays in local scope are left uninitialized).
C++ 不会初始化char
为' '
(ascii 值为 0x20,顺便说一句);事实上,没有指定用于初始化的数组初始化并不一定会发生(局部范围内的数组未初始化)。