110

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.

Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137

6 Answers6

159

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

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 3 in 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 beyond the last element.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 1
    Can you explain it please? I already read that `vector a(5,10);` mean `make room for 5 `int` and initialize them with 10. But how your x,x+... works? can you explain? – Asif Mushtaq Jan 16 '16 at 07:19
  • 1
    @UnKnown rather than selecting `vector::vector(size_type, int)`, it selects `vector::vector(int*, int*)`, which copies the range denoted by that pair of pointers. The first is overload (2), the second is overload (4) [here](http://en.cppreference.com/w/cpp/container/vector/vector) – Caleth Apr 13 '18 at 11:47
  • 2
    On c++11 std::extent is better than sizeof method. `sizeof x / sizeof x[0] == std::extent::value` – Isaac Pascual Jul 25 '18 at 16:09
145

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

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

std::vector<int> v({ 1, 2, 3, 4, 5 });
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • 3
    does it copy the array or it just points to it? i'm concerned with performance – kirill_igum Nov 07 '12 at 18:06
  • 3
    `std::vector` always owns the `T` objects. This has two implications: when inserting object into a vector they are copied and they are collocated in memory. For reasonably small objects, e.g. sequences of short strings, the collocation is a major performance gain. If your objects are big and expensive to copy, you might want to store [somehow resource managed] pointers to the objects. Which approach is more efficient depends on the objects but you have the choice. – Dietmar Kühl Nov 07 '12 at 22:16
  • so if i want to interface a c++ and a c libraries and copy from c-array to vector and back, there is no way of paying the penalty of 2 copies? (i'm using eigen library and gsl) – kirill_igum Nov 08 '12 at 00:14
22

Pointers can be used like any other iterators:

int x[3] = {1, 2, 3};
std::vector<int> v(x, x + 3);
test(v)
Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79
  • 3
    In the real life you may want to abstract out the array size, for example using `const size_t X_SIZE = 3;` for denoting the array size, or calculating it from sizeof. I omitted that part for sake of readability. – Rafał Rawicki Jan 08 '12 at 12:50
13

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)

Flexo
  • 87,323
  • 22
  • 191
  • 272
11

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

e.g.

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

vector<int> v;
v.assign(array, array+5); // 5 is size of array.
FooF
  • 4,323
  • 2
  • 31
  • 47
3251_ali
  • 147
  • 1
  • 10
1

One way can be to use the array's bound in one go like this:

 int a[3] = {1, 2, 3};
vector<int> v(a, *(&a+1));