ChatGPT解决这个技术问题 Extra ChatGPT

What is the simplest way to convert array to vector?

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.


F
Fred Foo

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.


Can you explain it please? I already read that vector<int> a(5,10); mean make room for 5 int` and initialize them with 10. But how your x,x+... works? can you explain?
@UnKnown rather than selecting vector<int>::vector(size_type, int), it selects vector<int>::vector(int*, int*), which copies the range denoted by that pair of pointers. The first is overload (2), the second is overload (4) here
On c++11 std::extent is better than sizeof method. sizeof x / sizeof x[0] == std::extent<decltype(x)>::value
D
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):

#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 });

does it copy the array or it just points to it? i'm concerned with performance
std::vector<T> 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.
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)
R
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)

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.
F
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)


"instead of forcing everything into a vector ask how you can convert test to work with iterators instead of a specific container." Why is this better?
@aquirdturtle because now, instead of only supporting vectors you support lists and arrays and boost containers and transform iterators and ranges and ....
and you didn't need to copy the data
F
FooF

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.

Equivalent to the use of the ctor, which was mentioned in existing answers some seven years ago. Adds nothing...
worthy when the ctor is not callable, for example vector v already be constructed in other places
G
Gyanendra Singh

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

Wow, welcome to Stack overflow, tested and approved