且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C++ 字符数组向量

更新时间:2023-11-10 08:00:22

您不能将数组存储在向量中(或任何其他标准库容器中).标准库容器存储的东西必须是可复制和可赋值的,而数组两者都不是.

You cannot store arrays in vectors (or in any other standard library container). The things that standard library containers store must be copyable and assignable, and arrays are neither of these.

如果您确实需要将数组放入向量中(而您可能不需要 - 使用向量向量或字符串向量更有可能是您需要的),那么您可以将数组包装在一个结构中:

If you really need to put an array in a vector (and you probably don't - using a vector of vectors or a vector of strings is more likely what you need), then you can wrap the array in a struct:

struct S {
  char a[10];
};

然后创建一个结构向量:

and then create a vector of structs:

vector <S> v;
S s;
s.a[0] = 'x';
v.push_back( s );