C++: Convert Array to Vector (7 Ways)

In this article, we will discuss different ways to convert an array to a vector in C++.

Table of Contents

Convert an array into a vector in C++ using Range Based Constructor

In C++. vector class provides a constructor which accepts a range i.e. [start, end). It creates a vector from all the element in the given range i.e. from start to end-1. So, to create a vector from all elements in an array, pass the array elements as range i.e. arr and arr + N, where N is the number of elements in array. For example,

#include<iostream>
#include<vector>

using namespace std;

int main()
{
    int arr[] = {1, 3, 4, 7, 8, 9};

    // Create a vector from array
    vector<int> numbers(arr, arr + sizeof(arr) / sizeof(int));

    // Print all elements of vector on console
    for(const auto & elem: numbers) {
        cout<<elem<< ", ";
    }
    
    return 0;
}

Output:

1, 3, 4, 7, 8, 9,

To calculate the number of elements (N) in array, we divided the size of array by the size of the type of elements in array. Then we passed the range arr & arr + N in the vector constructor to create a vector from the array.

Convert an array into a vector in C++ – begin() / end()

In the previous example, we calculated the size of array and the created a range with the help of it. But we can avoid doing all that stuff if we simply use begin() & end(),

  • std::begin(arr) -> Return an iterator pointing to the first element of the array.
  • std::end(arr) -> Return an iterator pointing to the one after the last element of the array.

Get the the start & end of an array using begin() and end(). Then pass that to the vector constructor to create a vector from an array. For example,

#include<iostream>
#include<vector>

using namespace std;

int main()
{
    int arr[] = {1, 3, 4, 7, 8, 9};

    // Create a vector from array
    vector<int> numbers(begin(arr), end(arr));

    // Print all elements of vector on console
    for(const auto & elem: numbers) {
        cout<<elem<< ", ";
    }
    
    return 0;
}

Output:

1, 3, 4, 7, 8, 9, 

Create an empty vector in C++ and add array elements to it

In vector, the insert() function accepts a position of the vector and a range [start, end) as arguments. Then inserts all the elements from the range into the vector at given position. We can create an empty vector and then using the insert() function of the vector we can add all elements of an array into the empty vector. For example,

#include<iostream>
#include<vector>

using namespace std;

int main()
{
    int arr[] = {1, 3, 4, 7, 8, 9};

    // Create an empty vector
    vector<int> numbers;
    // Add array elements to vector
    numbers.insert(numbers.begin(), begin(arr), end(arr));

    // Print all elements of vector on console
    for(const auto & elem: numbers) {
        cout<<elem<< ", ";
    }
    
    return 0;
}

Output:

1, 3, 4, 7, 8, 9,

Convert array to vector using for_each()

Just like the previous solution, create an empty vector, then iterate over all the elements in array using for_each() and apply a lambda function on each element. Inside the lambda function, we will add the element to the vector. For example,

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    int arr[] = {1, 3, 4, 7, 8, 9};

    // Create an empty vector
    vector<int> numbers;

    // Iterate over array elements and add 
    // them to vector
    for_each (  begin(arr),
                end(arr),
                [&](auto i) {
                    numbers.push_back(i);
                });
    
    // Print all elements of vector on console
    for(const auto & elem: numbers) {
        std::cout<<elem<< ", ";
    }
    
    return 0;
}

Output:

1, 3, 4, 7, 8, 9, 

C++: Convert an array to vector using copy() algorithm

Create an empty vector. Then pass the array elements as range [start, end) to the copy() function as initial two argument and as the third argument pass the back_insert_iterator of the vector. It will copy all the elements of array into the vector. For example,

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    int arr[] = {1, 3, 4, 7, 8, 9};

    // Create an empty vector
    vector<int> numbers;

    // Iterate over array elements and copy 
    // them to vector
    copy(   begin(arr),
            end(arr),
            back_inserter(numbers));


    // Print all elements of vector on console
    for(const auto & elem: numbers) {
        cout<<elem<< ", ";
    }
    
    return 0;
}

Output:

1, 3, 4, 7, 8, 9,

C++: Create empty vector and fill with items from array

Create an empty vector. Then pass the array elements as a range [start, end) into the assign() function of vector. Which copies them into the vector. For example,

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    int arr[] = {1, 3, 4, 7, 8, 9};

    // Create an empty vector
    vector<int> numbers;

    // Iterate over array elements and copy 
    // them to vector
    numbers.assign(begin(arr), end(arr));


    // Print all elements of vector on console
    for(const auto & elem: numbers) {
        cout<<elem<< ", ";
    }
    
    return 0;
}

Output:

1, 3, 4, 7, 8, 9,

C++: Convert array into vector and transform elements on the ways

In all the previous solutions, we copied elements from an array to the vector as it is. What if we want to change the elements while copying.
For example, suppose we have an array of integers and we want to convert it to a vector, but while adding them into the vector, we want to multiple each element by 2. To that we can use the transform() function. For example,

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main()
{
    int arr[] = {1, 3, 4, 7, 8, 9};

    // Create an empty vector
    vector<int> numbers;

    // Iterate over array elements and copy 
    // them to vector
    transform(  begin(arr),
                end(arr),
                back_inserter(numbers),
                [](const auto & elem){
                    return elem * 2;     
                });
    
    // Print all elements of vector on console
    for(const auto & elem: numbers) {
        cout<<elem<< ", ";
    }
    
    return 0;
}

Output:

2, 6, 8, 14, 16, 18,

transform() function, accepts an input range, an output iterator and a unary function. It the then iterates over all the elements in input range and applies the unary function on it and gives the result to output iterator.

As the input range we gave the array elements and then as output iterator we passed the vector iterator and as the unary function we passed a lambda function which returns the argument by multiplying it by two. It then iterated over all the elements in array and on each element it applied the lambda function and then adds them to vector.

Summary

We learned about different ways to create a vector object from an array in c++.

Advertisements

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top