Convert an array to a vector in C++
This post will discuss how to convert an array to a vector in C++.
1. Using Range Constructor
The idea is to use the vector’s range constructor that constructs a vector from elements of the specified range defined by two input iterators. This is the simplest and very efficient solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <vector> int main() { // input array int src[] = { 1, 2, 3, 4, 5 }; int n = sizeof(src) / sizeof(src[0]); std::vector<int> dest(src, src + n); for (int i: dest) { std::cout << i << " "; } return 0; } |
Output:
1 2 3 4 5
The above program uses the sizeof
operator for calculating the size of the array. We can avoid that in C++11, which introduced std::begin
and std::end
functions that return an iterator pointing to the beginning and the end of a sequence.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <vector> int main() { // input array int src[] = { 1, 2, 3, 4, 5 }; std::vector<int> dest(std::begin(src), std::end(src)); for (int i: dest) { std::cout << i << " "; } return 0; } |
Output:
1 2 3 4 5
2. Using std::insert
function
Another efficient solution is to use the std::insert
function. std::vector
has an overloaded version of std::insert
, which takes three parameters – the first parameter is an iterator to the destination vector, and the last two parameters are the iterators specifying a range of array elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <vector> int main() { // input array int src[] = { 1, 2, 3, 4, 5 }; std::vector<int> dest; dest.insert(dest.begin(), std::begin(src), std::end(src)); for (int i: dest) { std::cout << i << " "; } return 0; } |
Output:
1 2 3 4 5
3. Naive Solution
A naive solution is to use the simple for-loop that iterates the array and add all its values to the vector one at a time using the push_back()
function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <vector> int main() { // input array int src[] = { 1, 2, 3, 4, 5 }; // create an empty vector and push all elements std::vector<int> dest; for (int i: src) { dest.push_back(i); } for (int i: dest) { std::cout << i << " "; } return 0; } |
Output:
1 2 3 4 5
4. Using std::copy
function
We can also use the std::copy
STL algorithm, which copies the specified range of elements to another range beginning from the specified output iterator. Since we need to insert new elements at the end of the destination vector, we can use std::back_inserter
to the initial position in the destination.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <vector> #include <iterator> #include <algorithm> int main() { // input array int src[] = { 1, 2, 3, 4, 5 }; std::vector<int> dest; std::copy(std::begin(src), std::end(src), std::back_inserter(dest)); for (int i: dest) { std::cout << i << " "; } return 0; } |
Output:
1 2 3 4 5
If the vector has enough space, we can simply specify the output iterator to the beginning of the destination range.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <vector> #include <algorithm> int main() { // input array int src[] = { 1, 2, 3, 4, 5 }; int n = sizeof(src) / sizeof(src[0]); std::vector<int> dest(n); std::copy(src, src + n, dest.begin()); for (int i: dest) { std::cout << i << " "; } return 0; } |
Output:
1 2 3 4 5
5. Using memcpy()
function
Since the vector is nothing but a dynamic array, the memcpy()
function can also work here. It performs a binary copy of the data and hence is error-prone. It can be used with arrays of POD (Plain Old Data) type like int, char, etc., but not recommended with structs or classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <cstring> #include <vector> int main() { // input array int src[] = { 1, 2, 3, 4, 5 }; int n = sizeof(src) / sizeof(src[0]); std::vector<int> dest(n); memcpy(&dest[0], &src[0], n*sizeof(int)); for (int i: dest) { std::cout << i << " "; } return 0; } |
Output:
1 2 3 4 5
6. Using std::assign
function
We can also use the std::assign
function that replaces the existing contents of the vector with elements of the specified range.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <vector> int main() { // input array int src[] = { 1, 2, 3, 4, 5 }; std::vector<int> dest; dest.assign(std::begin(src), std::end(src)); for (int i: dest) { std::cout << i << " "; } return 0; } |
Output:
1 2 3 4 5
That’s all about converting an array to a vector in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)