In this article, we will discuss how to convert vectors to arrays in different ways in C++. But before going to its implementation, we must know about the arrays and vectors in C++.
Elements are stored in contiguous memory locations by both arrays and vectors. In C++, arrays are static and have fixed sizes. It means that once an array's size is stated in a program, it cannot be altered while the program is running, even if doing so would diminish the program's capacity.
On the other hand, vectors are dynamic arrays that are more adaptable to program requirements. They can dynamically resize themselves in response to program requirements, such as when an element is added or removed from the vector. Elements are kept in nearby memory regions in both arrays and vectors so that an iterator or pointer can access them.
Need for conversion-
- Compared to vectors, arrays are more efficient.
- Arrays access elements faster than vectors; i.e., arrays require less time to access elements than vectors do.
- Vectors use more memory.
- using for loop
- Using copy function
- Using transform function
- Using data function
Different methods of converting vector to arrays(4 ways)
Here, we will disucss these different methods one by one.
1. Using for loop
Let us take an example to illustrate how to convert vectors to arrays in C++ using for loops.
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v{7,9,8,1,1,7,7,9,7,5};
int n,i;
n=v.size();
int a[n];
for( i = 0; i < n; i++)
{
a[i] = v[i];
}
for(auto i: a) {
cout << i << endl ;
}
return 0;
}
Output:
2. Using copy function
Based on range, the C++ copy method can copy pieces between objects. For example, if we send the vector element to the copy function in the range [start, end] and iterate to the first array member, all of the vector's items will be copied to the array.
Example:
Let us take an example to illustrate how to convert vectors to arrays in C++ using copy function.
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v{7,9,8,1,1,7,7,9,7,5};
int arr[v.size()];
copy( v.begin(), v.end(), arr);
for(auto i: arr) {
cout<<i<<", ";
}
return 0;
}
Output:
3. Using transform function
The transform function is part of the C++ STL library. This function takes an input range, an output iterator, and a unary function as three arguments. It essentially applies a unary function, iterates over every element, and returns an output iterator.
Unary operation
This function stores the result in an output array and performs unary operations (ary_op) on members in the range [start, end]. It requires the pointer to the first array's start and end positions as well as the output array's start location.
Example:
int unary_op (int x) {
x = x * 2;
return x;
}
transform (arr1.start, arr1.end , output.start, unary_op);
Binary Operation
This function stores the result in an output array and performs binary operation binary_op on elements in the range [start, end]. It requires the pointer to the first array's start and end positions, the second array's start position, the output array, and the binary operation.
Example:
int binary_op (int i, int j) {
return i*j;
}
transform (arr1.start, arr1.end, arr2.start, output.start,binary_op );
Example:
Let us take an example to illustrate how to convert vectors to arrays in C++ using transform function.
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v{7, 9, 8, 1, 1, 7, 7, 9, 7, 5};
int arr[v.size()];
transform( v.begin(), v.end(), arr, [](const auto & ele){return ele; });
for(auto i: arr) {
cout<<i<<", ";
}
return 0;
}
Output:
4. Using data function
A pointer to the internal vector array is returned by the vector data method in C++. Because it returns an internal vector array in our instance, any modifications made to it will also affect our original vector.
Example:
Let us take an example to illustrate how to convert vectors to arrays in C++ using data function.
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> v {7, 9, 8, 1, 1, 7, 7, 9, 7, 5};
int *arr = v.data();
int size=v.size();
for(int i = 0; i < size; i++)
{
arr[i] = arr[i]*2;
cout<<arr[i]<<" ,";
}
cout<<endl;
cout<<"modified vectors elements - \n";
for(auto i : v)
cout<<i<<" ,";
return 0;
}
Output: