我可以将 int 数组推送到 C++ 向量吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11044304/
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
Can i push an array of int to a C++ vector?
提问by WoooHaaaa
Is there any problem with my code ?
我的代码有问题吗?
std::vector<int[2]> weights;
int weight[2] = {1,2};
weights.push_back(weight);
It can't be compiled, please help to explain why:
无法编译,请帮忙解释原因:
no matching function for call to ‘std::vector<int [2], std::allocator<int [2]> >::push_back(int*&)'
回答by Jesse Good
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:
数组不能在 STL 容器中使用的原因是它要求类型是可复制构造和可分配的(在 c++11 中也可移动构造)。例如,您不能对数组执行以下操作:
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 reallyneed 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;
However, it probably would be better if you used an std::vector
or std::array
.
但是,如果您使用std::vector
或可能会更好std::array
。
回答by vijay
You cant do that simply.
你不能简单地做到这一点。
It's better you use either of these:
最好使用以下任一方法:
vector <vector<int>>
(it's basically a two dimensional vector.It should work in your case)vector< string >
(string is an array of characters ,so you require a type cast later.It can be easily.).you can declare an structure (say S) having array of
int
type within it i.e.struct S{int a[num]}
,then declare vector ofvector< S>
vector <vector<int>>
(它基本上是一个二维向量。它应该适用于您的情况)vector< string >
(字符串是一个字符数组,因此您稍后需要进行类型转换。这很容易。)。您可以声明一个结构(比如 S),其中包含
int
类型数组,即struct S{int a[num]}
,然后声明向量vector< S>
So indirectly, you are pushing array into a vector.
因此,间接地,您将数组推入向量中。
回答by Dignesh P R
Array can be added to container like this too.
数组也可以像这样添加到容器中。
int arr[] = {16,2,77,29};
std::vector<int> myvec (arr, arr + sizeof(arr) / sizeof(int) );
Hope this helps someone.
希望这可以帮助某人。
回答by Mark B
Arrays aren't copy constructable so you can't store them in containers (vector
in this case). You can store a nested vector
or in C++11 a std::array
.
数组不可复制构造,因此您不能将它们存储在容器中(vector
在这种情况下)。您可以存储嵌套的vector
或在 C++11 中的 a std::array
。
回答by Alexey Samoylov
You should use std::array
instead of simple array:
您应该使用std::array
而不是简单的数组:
#include <vector>
#include <array>
std::vector<std::array<int, 2>> weights;
std::array<int, 2> weight = {1, 2};
weights.push_back(weight);
or with a constructor:
或使用构造函数:
std::vector<std::array<int, 2>> weights;
weights.push_back(std::array<int, 2> ({1, 2});
回答by jalsh
One possible solution is:
一种可能的解决方案是:
std::vector<int*> weights;
int* weight = new int[2];
weight[0] =1; weight[1] =2;
weights.push_back(weight);
回答by Arvind
Just use
只需使用
vector<int*> .That will definitely work.
A relevant discussion on the same topic : Pushing an array into a vector
关于同一主题的相关讨论:将数组推入向量
回答by Roger Dahl
To instantiate the vector, you need to supply a type, but int[2] is not a type, it's a declaration.
要实例化向量,您需要提供一个类型,但 int[2] 不是一个类型,它是一个声明。