Open In App

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

Last Updated : 23 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

An array is the collection of data belonging to a primitive data type. The data in this is kept in a continuous memory location. Vectors are dynamic arrays that can be resized to meet the needs. We can convert vectors to arrays in the following ways given below.

Example:

Input : Vector: [1, 2, 3, 4, 5]
Output: Array: [1, 2, 3, 4, 5]

Input : Vector: [‘G’, ‘e’, ‘e’, ‘k’, ‘s’] 
Output: Array: [‘G’, ‘e’, ‘e’, ‘k’, ‘s’] 

Here, we will discuss the 5 ways to convert Vector to Array in C++ STL:

  1. Naive Approach to Convert Vector to Array
  2. Using copy() function in C++ STL
  3. Using transform() function in C++ STL
  4. Using data() function in C++ STL
  5. Using & operator in C++

1. Naive Approach to Convert Vector to Array

The vector can be converted to an array by first allocating the array’s memory sufficient to accommodate all vector elements. Then we run a for loop and copy every element of the vector to the array.

Example:

C++




// C++ program to convert vector to
// array using naive approach
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    vector<int> v({ 1, 2, 3, 4, 5 });
    int n = v.size();
     
    // Printing original vector
    cout<<"Vector: ";
    for (int i: v) {
        cout << i << ' ';
    }
    cout<<endl;
  
    int arr[n];
    for (int i = 0; i < n; i++) {
        arr[i] = v[i];
    }
     
    // Printing the array
    cout<<"Array: ";
    for (int i: arr) {
        cout << i << ' ';
    }
    cout<<endl;
  
    return 0;
}


Output

Vector: 1 2 3 4 5 
Array: 1 2 3 4 5 

Time Complexity: O(n)
Auxiliary Space: O(n)

2. Using copy() function in C++ STL

copy() function in C++ is used to copy a range of elements from one container to another. It takes 3 arguments which are a pointer to the beginning of the source container, a pointer to the end of the source container, and a pointer to the beginning of the destination container.

Example:

C++




// C++ program to convert vector to
// array using copy() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Initialising the vector
    vector<int> v({ 1, 2, 3, 4, 5 });
    int n = v.size();
     
    // Printing original vector
    cout<<"Vector: ";
    for (int i: v) {
        cout << i << ' ';
    }
    cout<<endl;
     
    // Initialising the array to
    // store elements
    int arr[n];
     
    // Using copy() function
    // to copy elements
    copy(v.begin(),v.end(),arr);
     
    // Printing the array
    cout<<"Array: ";
    for (int i: arr) {
        cout << i << ' ';
    }
    cout<<endl;
  
    return 0;
}


Output

Vector: 1 2 3 4 5 
Array: 1 2 3 4 5 

Time Complexity: O(n)
Auxiliary Space: O(n)

3. Using transform() function in C++ STL

transform() function in C++ is used to copy a range of elements from one container to another based upon a unary operation. It takes 4 parameters which include a pointer to the beginning of the source container, a pointer to the end of the source container, and a pointer to the beginning of the destination container, a unary operation.

Example:

C++




// C++ program to convert vector to
// array using transform() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Initialising the vector
    vector<int> v({ 1, 2, 3, 4, 5 });
    int n = v.size();
     
    // Printing original vector
    cout<<"Vector: ";
    for (int i: v) {
        cout << i << ' ';
    }
    cout<<endl;
     
    // Initialising the array to
    // store elements
    int arr[n];
     
    // Using transform() function
    // to copy elements
    transform(v.begin(),v.end(),arr,[](const int & x){return x;});
     
    // Printing the array
    cout<<"Array: ";
    for (int i: arr) {
        cout << i << ' ';
    }
    cout<<endl;
  
    return 0;
}


Output

Vector: 1 2 3 4 5 
Array: 1 2 3 4 5 

Time Complexity: O(n)
Auxiliary Space: O(n)

4. Using data() function in C++ STL

data() function in C++ returns a pointer to the first element in the array which is used internally by the vector. There are no parameters accepted by this function.

Example:

C++




// C++ program to convert vector to
// array using data() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Initialising the vector
    vector<int> v{ 1, 2, 3, 4, 5 };
    int n = v.size();
 
    // Printing original vector
    cout << "Vector: ";
    for (int i : v) {
        cout << i << ' ';
    }
    cout << endl;
 
    // memory pointer pointing to the
    // first element of array
    int* arr = v.data();
 
    // Printing the array
    cout << "Array: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
 
    return 0;
}


Output

Vector: 1 2 3 4 5 
Array: 1 2 3 4 5 

Time Complexity: O(n)
Auxiliary Space: O(1)

Note: Be aware that in case of STL SequenceContainers which can change their size at runtime, like std::vector, there is no guarantee that the memory location of the underlying array will stay same across container manipulations that change the containers size.

5. Using & operator in C++

When used on the first element of an STL SequenceContainer (std::vector, std::array…), the & operator returns a pointer to the first element of an internally used C-style array. That array can be accessed and modified directly. 

Example:

C++




// C++ program to convert vector to
// array using transform() function
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Initialising the vector
    vector<int> v{ 1, 2, 3, 4, 5 };
    int n = v.size();
     
    // Printing original vector
    cout<<"Vector: ";
    for (int i: v) {
        cout << i << ' ';
    }
    cout<<endl;
     
    // returning the address
    // of the first element
    // of vector to array
    int * arr = &v[0];
     
    // Printing the array
    cout<<"Array: ";
    for(int i=0;i<n;i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
  
    return 0;
}


Output

Vector: 1 2 3 4 5 
Array: 1 2 3 4 5 

Time Complexity: O(n)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads