c++ 中的字符串数组初始化
我知道我可以像这样初始化,
std::string foo [3] = {"T1","T2","T3"};
但是如果之前声明它,我必须单独初始化每个吗? 例如
std::string foo [3];
foo[0] = "T1"
foo[1] = "T2"
foo[2] = "T3"
我的意思是一次给字符串所有值 foo ={"T1","T2","T3"}
i know i can initialize like this
std::string foo [3] = {"T1","T2","T3"};
but what if declared it before do i have to initialize each one by its own?
example
std::string foo [3];
foo[0] = "T1"
foo[1] = "T2"
foo[2] = "T3"
i mean giving the string all the values at once foo ={"T1","T2","T3"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从技术上讲,您无法在声明后对其进行初始化。细微差别很重要,因为在第二个示例中您分配给各个字符串。现在,您想分配给整个数组,这是行不通的。如果您使用更智能的数据结构,例如 std::vector ,您确实可以在一条指令中分配所有值,因为向量会丢弃旧字符串并创建新字符串,这些新字符串将从您的
“T1”
,..字符串。Technically, you cannot initialise it after the declaration. The nuance is important, because in your second example you assign to the individual strings. Now, you want to assign to the whole array, which doesn't work. If you used a more intelligent data structure like
std::vector
you could indeed assign all values in one instruction, because the vector would throw away the old strings and create new ones that would be copy constructed from your"T1"
,.. strings.您不会单独初始化每一个。它们被初始化为空字符串(默认构造),然后您可以为它们赋值。
You are not initializing each one by its own. They are initialized to the empty string (default constructed), and you are then assigning values to them.
不,(与 Java 不同),当您声明数组时,它会使用默认构造函数在每个索引处创建一个字符串。
为了清楚起见进行编辑:
我所说的“新”并不是指像堆的新分配那样,它存储在您声明它的任何位置,在数据部分(对于全局变量)或堆栈(局部变量)中。
从评论来看,您似乎更多地考虑了这样的事情:
不,您不能这样做。
foo
已经分配并初始化了其空间,并且您不能像这样分配整个数组(初始化程序中除外)。如果您将其声明为std::string *foo
则可以,但在这种情况下我不建议这样做。No, (unlike Java), when you declare the array, it creates a string at each index using the default constructor.
Edit for clarity:
by "new" I don't mean like the
new
allocation to the heap, it gets stored wherever you declared it, in the data section (for globals) or stack (locals).From a comment, it seems you're thinking more of something like this:
No, you can't do that.
foo
already has its space allocated and initialized, and you can't just assign whole arrays like that (except in the initializer). If you declared it asstd::string *foo
you could, but I don't recommend that in this case.我仍在学习 C++,经验不是很丰富,但我相信如果您想在声明后进行初始化,这可能是最好的方法。或者您可以使用 for 循环来填充它。
I'm still learning C++ and I'm not very experienced, but I believe that is perhaps the best way if you wanted to initialize after the declaration. Or you could use a for loop for example to populate it.
在 C++0x 中,您将能够使用通用初始值设定项列表。但这与手写作业没有太大区别。
但请注意,如果您为每个赋值重复了一个复杂的表达式(正如 K-ballo 指出的,赋值和初始化之间的区别有时很重要,请养成区分的习惯):
您可以使用引用缩短它:
这消除了很多痛苦。
In C++0x, you'll be able to use generalized initializer lists. But it won't be much different from writing the assignments out longhand.
Do note, however, that in the case where you had a complicated expression repeated for each assignment (as K-ballo points out, the different between assignment and initialization is sometimes important, get in the habit of distinguishing):
you can use a reference to shorten that up:
which takes away much of the pain.