In this article we will discuss different ways to convert a vector into an array in C++.

## Convert a vector into an array by copying items one by one

Suppose we have a vector of N integers and we want to create an array from it. For that first we need to create a n array of size N. Then we can iterate over all the elements of vector and copy them one by one into the array. For example,

#include<iostream> #include<vector> int main() { // Create a vector of integers std::vector<int> vec_of_num{1, 3, 4, 7, 8, 9}; // Create an array of size equivalent to vector int arr[vec_of_num.size()]; // Iterate over vector and copy elements to array for(int i = 0; i < vec_of_num.size(); i++) { arr[i] = vec_of_num[i]; } // Iterate over the array and print the contents for(auto x: arr) { std::cout<<x<<", "; } return 0; }

Output:

1, 3, 4, 7, 8, 9,

## Convert a vector into an array using STL Algorithm copy()

Create an array of same size as the vector. Then pass the vector elements as range [start, end) to the copy() function as initial two argument and as the third argument pass the iterator pointing to the start of array. It will copy all the elements of vector into the array. For example,

#include<iostream> #include<vector> #include<algorithm> int main() { // Create a vector of integers std::vector<int> vec_of_num{1, 3, 4, 7, 8, 9}; // Create an array of size equivalent to vector int arr[vec_of_num.size()]; // Copy all elements of vector to array std::copy( vec_of_num.begin(), vec_of_num.end(), arr); // Iterate over the array and print the contents for(auto x: arr) { std::cout<<x<<", "; } return 0; }

Output:

1, 3, 4, 7, 8, 9,

## Convert a vector into an array using STL Algorithm transform()

STL algorithm transform() function accepts an input range, an output iterator and a unary function as arguments. It the then iterates over all the elements in input range and applies the unary function on each of them. Then gives the values returned by lambda function to the output iterator.

Basically it transforms the values in input range using callback function and stores them to the other container. Let’s use it to convert a vector into an array in C++,

#include<iostream> #include<vector> #include<algorithm> int main() { // Create a vector of integers std::vector<int> vec_of_num{1, 3, 4, 7, 8, 9}; // Create an array of size equivalent to vector int arr[vec_of_num.size()]; // Copy all elements of vector to array std::transform( vec_of_num.begin(), vec_of_num.end(), arr, [](const auto & elem){ return elem; }); // Iterate over the array and print the contents for(auto x: arr) { std::cout<<x<<", "; } return 0; }

Output

### Frequently Asked:

- How to print Two Dimensional (2D) Vector in C++ ?
- vector::pop_back() function in C++
- Check if a vector is empty in C++
- Difference between Vector and List in C++

1, 3, 4, 7, 8, 9,

We created an array of same size as vector. Then we called the transform() function with following arguments,

- As the input range passed the vector elements
- As output iterator passed the iterator pointing to the start of an array.
- As the unary function we passed a lambda function which returns the copy of argument.

transform() function iterated over all the elements in vector and on each element it applied the lambda function and then copied the values returned by lambda function into the array.

## C++: Convert a vector into an array using vector::data()

Vector provides a function data(), which returns a pointer to the internal array of vector. For example,

int * refArr = vec_of_num.data();

But be careful, as it returns the internal array of vector. So any modification in it, will modify the values of vector too. For example,

#include<iostream> #include<vector> #include<algorithm> int main() { // Create a vector of integers std::vector<int> vec_of_num{1, 3, 4, 7, 8, 9}; int * refArr = vec_of_num.data(); int size = vec_of_num.size(); for(int i = 0; i < size; i++) { refArr[i] = refArr[i] + 10; std::cout<<refArr[i]<<", "; } std::cout<<std::endl; for(auto x: vec_of_num) { std::cout<<x<<", "; } return 0; }

Output:

11, 13, 14, 17, 18, 19, 11, 13, 14, 17, 18, 19,

Here we fetched the internal array of vector and add 10 to each value. It modified the values of vector too.

## Summary

We learned about different ways to convert a vector into an array in C++.