3
vector<int> array2vector(int array[]) {
    int l = sizeof(array) / sizeof(array[0]);
    vector<int> result(array, array + l);
    return result;
}

int main() {
    int my_array[] = { 1,3,5,2,6 };
    vector<int> baby = array2vector(my_array);
    for (int i : baby) {
        cout << i << endl;
    }
    return 0;
}

There have been many ways to convert an int array to a vector, but the examples always starts with initializing an int array, and then converting it into a vector. However, I'd like to know if a function takes in an int array, and I want to convert it into a vector within the function, how I should do it?

The above code has the problem that sizeof(array) is "Diving size of a pointer by another value". The code prints out

1
3

instead of the entire array.

  • 1
    `int array[]` will decay to `int* array` in `array2vector` and you will lose information about the array, which is one of the reasons using arrays are bad. – Tas Jun 10 '20 at 00:21
  • also see: https://stackoverflow.com/a/7439261/4342498 – NathanOliver Jun 10 '20 at 00:22
  • You don't need a function for this. `std::vector` has a constructor that takes iterators as input. You can get iterators for a fixed array using `std::begin()`/`std::end()`, eg: `std::vector baby(std::begin(my_array), std::end(my_array));` or you can just use raw pointers instead, eg: `std::vector baby(my_array, my_array + std::size(my_array));` Or, `my_array` could be changed to a `std::array` instead, which has its own iterators, eg: `std::array my_array = { 1,3,5,2,6 }; std::vector baby(my_array.begin(), my_array.end());` – Remy Lebeau Jun 10 '20 at 00:25
  • I know I don't need a function for this, but I'm just worried if I were asked(such as a leetcode question) to write a function that explicitly demands the input to be an int array, and I want to convert the array into a vector within the function. So in short, I guess I won't be asked such questions? – Peter Zhong Jun 10 '20 at 00:42
  • C-style array with size as a parameter: https://stackoverflow.com/questions/3368883/how-does-this-size-of-array-template-function-work – Eljay Jun 10 '20 at 00:58
  • 1
    Here is how I would make it: `template std::vector array2vector(T (&array)[N]) {return std::vector(array, array + N);}` Here is a demo: https://onlinegdb.com/r1LVnna2L This works because we don't pass the array (which would decay to a pointer) but instead we pass a reference to the array - but each array size is a separate type so we need a template. To be fair if you wanted to constrain it to int then you could `template std::vector array2vector(int (&array)[N]) {return std::vector(array, array + N);}` – Jerry Jeremiah Jun 10 '20 at 01:49
  • 1
    @JerryJeremiah - What you say is correct. Need to note that a template function is a family of functions, with one function created for every combination of parameters (e.g. passing an array of two `int` to your templated `array2vector()` and passing an array of three `int` actually creates two distinct functions (both instantiations of the template). And it's not possible for the size of a passed array to be determined at run time - it must be known to the compiler at each call point. – Peter Jun 10 '20 at 02:27
  • 1
    If you read the question and, if you want to answer the question, then yes, there is an answer: Yes, there is an existing function. It is called std::transform. If you want to write an own function, then you will find answers in the comments. Btw. The mentioned dupes do not answer the question. The OP does not like it, but the above mentioned range constructor is the best solution – A M Jun 10 '20 at 05:52

0 Answers0