C++ 将数组转换为向量的最简单方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8777603/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:05:33  来源:igfitidea点击:

What is the simplest way to convert array to vector?

c++arraysvector

提问by Amir Saniyan

What is the simplest way to convert array to vector?

将数组转换为向量的最简单方法是什么?

void test(vector<int> _array)
{
  ...
}

int x[3]={1, 2, 3};
test(x); // Syntax error.

I want to convert x from int array to vector in simplest way.

我想以最简单的方式将 x 从 int 数组转换为向量。

回答by Fred Foo

Use the vectorconstructor that takes two iterators, note that pointers are valid iterators, and use the implicit conversion from arrays to pointers:

使用vector带有两个迭代器的构造函数,注意指针是有效的迭代器,并使用从数组到指针的隐式转换:

int x[3] = {1, 2, 3};
std::vector<int> v(x, x + sizeof x / sizeof x[0]);
test(v);

or

或者

test(std::vector<int>(x, x + sizeof x / sizeof x[0]));

where sizeof x / sizeof x[0]is obviously 3in this context; it's the generic way of getting the number of elements in an array. Note that x + sizeof x / sizeof x[0]points one element beyondthe last element.

其中,sizeof x / sizeof x[0]显然是3在这种背景下,这是获取数组中元素数量的通用方法。请注意,x + sizeof x / sizeof x[0]指向最后一个元素之外的一个元素。

回答by Dietmar Kühl

Personally, I quite like the C++2011 approach because it neither requires you to use sizeof()nor to remember adjusting the array bounds if you ever change the array bounds (and you can define the relevant function in C++2003 if you want, too):

就我个人而言,我非常喜欢 C++2011 方法,因为它既不需要您使用,sizeof()也不需要记住在您更改数组边界时调整数组边界(如果您愿意,也可以在 C++2003 中定义相关函数) ):

#include <iterator>
#include <vector>
int x[] = { 1, 2, 3, 4, 5 };
std::vector<int> v(std::begin(x), std::end(x));

Obviously, with C++2011 you might want to use initializer lists anyway:

显然,对于 C++2011,您可能无论如何都想使用初始化列表:

std::vector<int> v({ 1, 2, 3, 4, 5 });

回答by Rafa? Rawicki

Pointers can be used like any other iterators:

指针可以像任何其他迭代器一样使用:

int x[3] = {1, 2, 3};
std::vector<int> v(x, x + 3);
test(v)

回答by Flexo

You're asking the wrong question here - instead of forcing everything into a vector ask how you can convert test to work with iterators instead of a specific container. You can provide an overload too in order to retain compatibility (and handle other containers at the same time for free):

您在这里问了错误的问题 - 而不是将所有内容强制转换为向量,而是询问如何将测试转换为使用迭代器而不是特定容器。您也可以提供重载以保持兼容性(并免费同时处理其他容器):

void test(const std::vector<int>& in) {
  // Iterate over vector and do whatever
}

becomes:

变成:

template <typename Iterator>
void test(Iterator begin, const Iterator end) {
    // Iterate over range and do whatever
}

template <typename Container>
void test(const Container& in) {
    test(std::begin(in), std::end(in));
}

Which lets you do:

这让您可以:

int x[3]={1, 2, 3};
test(x); // Now correct

(Ideone demo)

( Ideone 演示)

回答by 3251_ali

One simple way can be the use of assign()function that is pre-defined in vectorclass.

一种简单的方法是使用assign()vector类中预定义的函数。

e.g.

例如

array[5]={1,2,3,4,5};

vector<int> v;
v.assign(array, array+5); // 5 is size of array.