Table of Contents
In this post, we will see how to convert vector to Array in C++.
Vectors and Arrays in C++
We can use an array to store a collection of data belonging to a primitive data type. It stores the data in a contiguous memory location. Vectors are very similar to arrays.
We can assume vectors to be dynamic arrays that can be resized according to the requirements. They are not present in C and are only available in C++. There are several methods associated with vectors in C++.
Convert Vector to Arrays in C++
In this article, we will discuss different methods to convert vector to array in C++.
Using the for
loop to convert vector to array in C++
In this method, we will traverse through the vector and copy every element individually to a new array.
See the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<iostream> #include<vector> using namespace std; int main() { std::vector<int> v{5,7,8,2}; int a[v.size()]; for(int i = 0; i < v.size(); i++) { a[i] = v[i]; } for(auto i: a) { cout << i << endl ; } return 0; } |
Output:
7
8
2
In the above example,
- We defined a vector
v
that stores some integers. - Using the
size()
function we find the length of the vector. - We create an array
a
of the same size. - We use the
for
loop to iterate over the vector and assign every element to the array. - The array now contains all the elements of the vector and we display it.
Further reading:
Using the copy()
function to convert vector to array in C++
In C++, the copy()
function can copy the elements from one object to another based on some provided range. It is defined in the algorithm
header file.
For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include<iostream> #include<vector> using namespace std; int main() { std::vector<int> v{5,7,8,2}; int a[v.size()]; copy(v.begin(), v.end(), a); for(auto i: a) { cout << i << endl ; } return 0; } |
Output:
7
8
2
In the above example,
- The
copy()
function will copy the elements from the vectorv
to the arraya
. - In the
copy()
function, thev.begin()
function provides the iterator pointing to the initial element of vectorv
. - Similarly, the
end()
function returns the iterator pointing to the last element.
Using the transform()
function to convert vector to array in C++
We can use the transform()
function in C++ to apply a function to all the elements of an array. It applies the function to all the elements and stores the result in a new array.
We can use it to convert vector to array. We will apply a single-line function that returns the element of the vector and store it in the array.
Here also we will specify the range of elements using the begin()
and end()
function, as we did in the previous method.
See the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { std::vector<int> v{5,7,8,2}; int a[v.size()]; transform(v.begin(), v.end(), a,[](const int & elem){return elem;}); for(auto i: a) { cout << i << endl ; } return 0; } |
Output:
7
8
2
Using the data()
function to convert vector to array in C++
We can use the data()
function to return the memory location of the vector in a double
pointer. We can assign this to a pointer and represent the internal array of the vector. Any modifications made to this will be reflected in the original vector.
See the following example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { std::vector<int> v{5,7,8,2}; int * a = v.data(); for (size_t i = 0; i < v.size(); ++i) { cout << a[i] << endl ; } return 0; } |
Output:
7
8
2
In the above code,
- The
data()
function returns the memory address of vectorv
. - We use this to create an array instance.
- The
size_t
in thefor
loop is used to specify the byte size of memory to iterate over using memory location and not the index.
Using the &
operator to convert vector to array in C++
The &
operator returns the address of an object in C++. As discussed in the previous method, we will return the address of the first element of the vector to a pointer to represent the internal array structure of a vector.
For example,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main() { std::vector<int> v{5,7,8,2}; int * a = &v[0]; for (size_t i = 0; i < v.size(); ++i) { cout << a[i] << endl ; } return 0; } |
Output:
7
8
2
As discussed in the previous method, any changes made will reflect on the original vector.
Conclusion
In this article, we discussed how to convert vector to array in C++. First, we discussed arrays and vectors noting their similarities and differences. The initial methods involving the for
loop, copy()
function, and transform()
function created a new array using the elements of the vector.
The final two methods achieved this using the memory location of the vector, so any changes made in the array were reflected in the original vector.
That’s all about how to convert Vector to Array in C++.