c++ 中的字符串数组初始化

发布于 2024-12-14 09:02:16 字数 255 浏览 0 评论 0原文

我知道我可以像这样初始化,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

咽泪装欢 2024-12-21 09:02:16

从技术上讲,您无法在声明后对其进行初始化。细微差别很重要,因为在第二个示例中您分配给各个字符串。现在,您想分配给整个数组,这是行不通的。如果您使用更智能的数据结构,例如 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.

梅倚清风 2024-12-21 09:02:16

如果之前声明了它,我必须单独初始化每个吗?

您不会单独初始化每一个。它们被初始化为空字符串(默认构造),然后您可以为它们赋值。

what if declared it before do i have to initialize each one by its own?

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.

晨曦慕雪 2024-12-21 09:02:16

不,(与 Java 不同),当您声明数组时,它会使用默认构造函数在每个索引处创建一个字符串。

为了清楚起见进行编辑:

我所说的“新”并不是指像堆的新分配那样,它存储在您声明它的任何位置,在数据部分(对于全局变量)或堆栈(局部变量)中。


从评论来看,您似乎更多地考虑了这样的事情:

std::string foo[3];
...
foo = {"a","b","c"}; // DOES NOT WORK

不,您不能这样做。 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:

std::string foo[3];
...
foo = {"a","b","c"}; // DOES NOT WORK

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 as std::string *foo you could, but I don't recommend that in this case.

懵少女 2024-12-21 09:02:16

我仍在学习 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.

一身骄傲 2024-12-21 09:02:16

在 C++0x 中,您将能够使用通用初始值设定项列表。但这与手写作业没有太大区别。

但请注意,如果您为每个赋值重复了一个复杂的表达式(正如 K-ballo 指出的,赋值和初始化之间的区别有时很重要,请养成区分的习惯):

ALongPointerName->ALongMemberName.ALongArrayName[0] = "T1";
ALongPointerName->ALongMemberName.ALongArrayName[1] = "T2";
ALongPointerName->ALongMemberName.ALongArrayName[2] = "T3";

您可以使用引用缩短它:

auto& a = ALongPointerName->ALongMemberName.ALongArrayName;
a[0] = "T1";
a[1] = "T2";
a[2] = "T3";

这消除了很多痛苦。

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):

ALongPointerName->ALongMemberName.ALongArrayName[0] = "T1";
ALongPointerName->ALongMemberName.ALongArrayName[1] = "T2";
ALongPointerName->ALongMemberName.ALongArrayName[2] = "T3";

you can use a reference to shorten that up:

auto& a = ALongPointerName->ALongMemberName.ALongArrayName;
a[0] = "T1";
a[1] = "T2";
a[2] = "T3";

which takes away much of the pain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文