In this article, we are going to learn different ways to convert a vector to a array in C++. Both vector and array are different data structures and have their own advantages and disadvantages.
Using data() Method to Convert a Vector to an Array
Vector supports many in built functions. data() function is also an inbuilt function in vector class. We can use this function to get the pointer to the first element of the vector. Vectors are stored in memory in continuous memory locations so this pointer will help us to iterate over the elements of this vector.
Once we get the pointer then we can use the array indexing to print the vector like an array.
Also , we can declare an array variable and copy the vector elements to this array. This will also give us the array which is exact copy of the vector.
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int> vec {1, 2, 3, 4, 5, 6,7,8};
cout <<"Vector Elements are :";
for (size_t i = 0; i < vec.size(); ++i) {
cout << vec[i] << "; ";
}
cout << endl;
const int ARRAY_SIZE = vec.size();
int arr1 [ARRAY_SIZE];
int *arr = vec.data();
cout <<"Array Elements in Memory :";
for (size_t i = 0; i < vec.size(); ++i) {
cout << arr[i] << "; ";
arr1[i] = arr[i];
}
cout << endl;
cout <<"Array Elements are :";
for (size_t i = 0; i < vec.size(); ++i) {
cout << arr1[i] << "; ";
}
return 0;
}
Output:
Vector Elements are :1; 2; 3; 4; 5; 6; 7; 8;
Array Elements in Memory :1; 2; 3; 4; 5; 6; 7; 8;
Array Elements are :1; 2; 3; 4; 5; 6; 7; 8;
Using & Address of Operator to Convert a Vector to an Array
Using the same concept of memory address, we can get the first element memory address by using the & operator. Once we get the pointer then we can use the array indexing to print the vector like an array.
Also , we can declare an array variable and copy the vector elements to this array. This will also give us the array which is exact copy of the vector.
So in this code, we basically just changed one line to replace the data() function with & operator.
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int> vec {1, 2, 3, 4, 5, 6,7,8};
cout <<"Vector Elements are :";
for (size_t i = 0; i < vec.size(); ++i) {
cout << vec[i] << "; ";
}
cout << endl;
const int ARRAY_SIZE = vec.size();
int arr1 [ARRAY_SIZE];
int *arr = &vec[0];
cout <<"Array Elements in Memory :";
for (size_t i = 0; i < vec.size(); ++i) {
cout << arr[i] << "; ";
arr1[i] = arr[i];
}
cout << endl;
cout <<"Array Elements are :";
for (size_t i = 0; i < vec.size(); ++i) {
cout << arr1[i] << "; ";
}
return 0;
}
Output:
Vector Elements are :1; 2; 3; 4; 5; 6; 7; 8;
Array Elements in Memory :1; 2; 3; 4; 5; 6; 7; 8;
Array Elements are :1; 2; 3; 4; 5; 6; 7; 8;
Using copy() Function to Convert a Vector to an Array
In this example, we are using the STL copy() function to copy the vector elements to an array. We can get the vector size to declare the array variable with that size. Then using the copy() function we can copy the elements to the array.
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int> vec {1, 2, 3, 4, 5, 6,7,8};
cout <<"Vector Elements are :";
for (size_t i = 0; i < vec.size(); ++i) {
cout << vec[i] << "; ";
}
cout << endl;
const int ARRAY_SIZE = vec.size();
int arr [ARRAY_SIZE];
copy(vec.begin(), vec.end(), arr);
cout << endl;
cout <<"Array Elements are :";
for (size_t i = 0; i < vec.size(); ++i) {
cout << arr[i] << "; ";
}
return 0;
}
Output:
Vector Elements are :1; 2; 3; 4; 5; 6; 7; 8;
Array Elements are :1; 2; 3; 4; 5; 6; 7; 8;