且构网

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

我可以把一个int数组到一个C ++向量?

更新时间:2022-12-10 23:19:37

数组不能在STL容器中使用的原因是因为它需要类型是可复制构造和可分配的(在c ++ 11中也可移动构造)。例如,您不能使用数组执行以下操作:

The reason arrays cannot be used in STL containers is because it requires the type to be copy constructible and assignable (also move constructible in c++11). For example, you cannot do the following with arrays:

int a[10];
int b[10];
a = b; // Will not work!

由于数组不满足要求,因此不能使用它们。但是,如果您真的需要使用数组(可能不是这样),您可以将其添加为类的成员:

Because arrays do not satisfy the requirements, they cannot be used. However, if you really need to use an array (which probably is not the case), you can add it as a member of a class like so:

struct A { int weight[2];};
std::vector<A> v;

但是,如果使用 std :: vector std :: array