In this post, we will explore various methods to transform vectors into arrays in C++. However, prior to delving into the practical aspects, it is essential to understand the concepts of arrays and vectors in C++.
Elements in arrays and vectors are both stored in adjacent memory locations. In C++, arrays are considered static with a predetermined size that cannot be changed during program execution, regardless of any potential impact on the program's efficiency.
On the flip side, vectors are flexible arrays that can easily adjust to the needs of a program. They have the capability to automatically resize based on program demands, for instance, when an element is inserted or deleted from the vector. Elements in both arrays and vectors are stored in close memory locations to facilitate access by iterators or pointers.
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 explore these various techniques individually.
1. Using for loop
Let's consider a scenario to demonstrate the process of transforming vectors into arrays in C++ by utilizing 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
By specifying a range, the C++ copy function is capable of duplicating segments of data from one object to another. For instance, providing a vector element to the copy method within the specified range [start, end] and starting iteration from the initial array element will result in transferring all elements from the vector to the array.
Example:
Let's consider an instance to demonstrate the conversion of vectors to arrays in C++ by utilizing the 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 a component of the C++ STL library. This function accepts three arguments: an input range, an output iterator, and a unary function. Its primary role is to execute a unary function, traverse through each element, and provide an output iterator as a result.
Unary operation
This function saves the outcome in an output array and executes unary operations (ary_op) on elements within the specified range [start, end]. It necessitates the pointer to the initial array's start and end positions, along with the starting point of the output array.
Example:
int unary_op (int x) {
x = x * 2;
return x;
}
transform (arr1.start, arr1.end , output.start, unary_op);
Binary Operation
This function saves the outcome in an output array and executes a binary operation named binary_op on elements within the specified range [start, end]. It necessitates the pointer to the initial array's start and end points, the commencement point of the second array, 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's consider an example to demonstrate the process of converting vectors into arrays in C++ by employing the 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
The data method in C++ returns a pointer to the internal vector array. Modifying this array will directly impact the original vector since it provides access to the internal data structure.
Example:
Let's consider a scenario to demonstrate the process of converting vectors to arrays in C++ by utilizing the 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: