Convert an array to vector in C++

This article is going to show how to convert an array to a vector in C++ with the help of examples.

The vector is a dynamic array. Thus, we can convert an array to a vector without data loss. This conversion is useful in cases when the input of the function requires a vector and the data is available in an array. I will demonstrate the different ways of doing this conversion.

While creating a vector:

The vector class has several constructors and one of its constructors takes in an iterator of other range containers (eg. set, array) as input.

The syntax for the constructor is as given here:

std::vector::vector<typename>(start_iterator,end_iterator);

The elements in the range [start, end) are added to the vector.

As we know that the array pointers can be used as iterators, we can use the pointer to the first element and the last element as the start and end iterator.

The following example illustrates the implementation:

#include <iostream>
#include <vector>
int main() {
    //converting c array
int array[5]={0,1,2,3,4}; // array to be converted to vector
std::vector<int> ans1(array,array+5); // c array to 
std::cout<<"The vector is: ";
for(int x:ans1)
{
    std::cout<<x<<" ";
}
std::cout<<"\n";
}

Below is given what you see in the output running the above program:

The vector is: 0 1 2 3 4

In the above example, I have used a normal c style array. If you want to convert an STL array container to a vector then you can use the iterator provided by it. It is possible to use the begin() function to get the iterator to the first element and the end() for the iterator to the last element.

Using the insert function:

The insert() function of the vector has an overload that takes iterators as input and inserts them into the vector. If you have an empty vector then you can insert all the elements of an array into it using the C++ insert function. With the help of this method we are essentially converting the array to a vector.

The following example illustrates the implementation :

#include <iostream>
#include <vector>
int main() {
    //converting c array
int array[5]={0,1,2,3,4}; // array to be converted to vector
std::vector<int> ans1;
ans1.insert(ans1.begin(),array,array+5); //c array to vector
std::cout<<"The vector is: ";
for(int x:ans1)
{
    std::cout<<x<<" ";
}
std::cout<<"\n";
}

Look at the result for this time:

The vector is: 0 1 2 3 4

And again, here we have demonstrated with c array. If you want to convert an STL array, you can resort to its iterator class.

If you ask me, you should use the constructor version because it is easier to interpret when other people read the code.

You can also use manual methods like looping over the array and inserting elements one by one. But that reduces code readability drastically. So I am not going to show you this process.

In conclusion, It is always better to use the STL function to convert an array to a vector.

Leave a Reply

Your email address will not be published. Required fields are marked *