Open In App

Different Ways to Convert Vector to Array in C++ STL

Last Updated : 21 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, vectors are dynamic arrays that can resize according to the number of elements. In this article, we will learn how to convert a vector to a C-style array in C++.

The easiest way to convert a vector to array is by creating a new array and copy all the elements of vector into it using copy() function. Let’s look at an example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v({ 1, 2, 3, 4, 5 });
    int n = v.size();
    
    // Create an array of same size
    int arr[n];
    
    // Using copy() function to copy elements
    copy(v.begin(),v.end(),arr);
    
    for (int i: arr)
        cout << i << ' ';
    return 0;
}

Output
1 2 3 4 5 

Explanation: The copy() function copies all the elements from the given range to the given container or array using iterators to define the range.

C++ also provides a lot of different ways to convert vector to array. Some of the common methods are as follows:

Using transform()

The transform() function is similar to copy() function but it also applies the given operation to each of the element. It can be used to convert a vector to an array by specifying a no-operation function if you just want to convert vector as it is.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v({ 1, 2, 3, 4, 5 });
    int n = v.size();
    
    // Create an array of same size
    int arr[n];
    
    // Using transform() function 
    // to copy elements
    transform(v.begin(),v.end(),arr,[](const int& x){
      	return x;
    });
    
    for (int i: arr)
        cout << i << ' ';
    return 0;
}

Output
1 2 3 4 5 

Using Vector data()

Vector internally uses a dynamically allocated array, and the vector data() function returns a pointer to the first element of this internal array.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v{ 1, 2, 3, 4, 5 };
    int n = v.size();

    // Memory pointer pointing to the
    // first element of internal array
    int* arr = v.data();

    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    return 0;
}

Output
1 2 3 4 5 

This method doesn’t actually convert the vector but instead work by providing the internal array of the vector.

Note: Be aware that vector can change their size at runtime, so there is no guarantee that the memory location of the underlying array will stay same.

& operator in C++

The & addressof operator can be used to find the pointer to the first element of an internally array.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v{ 1, 2, 3, 4, 5 };
    int n = v.size();
    
    // Finding the address of first element
    // of vector to array
    int * arr = &v[0];
  
    for(int i = 0;i < n; i++)
        cout << arr[i] << " ";

    return 0;
}

Output
1 2 3 4 5 




Next Article

Similar Reads