In this C++ tutorial, you will learn how to convert a built-in array to a std::vector. We cover the vector range constructor, copying only part of an array, assign(), insert(), and a loop with push_back().
Convert Array to Vector in C++
The most direct way to convert a C++ array to a vector is to pass the beginning and ending of the array to the vector range constructor. You can also use assign() when the vector already exists, insert() when array elements should be appended to an existing vector, or copy elements one at a time with push_back().
For a built-in array named arr, the common range-constructor form is:
std::vector<int> values(std::begin(arr), std::end(arr));
This creates a new vector containing copies of the array elements in the same order. The source array and the vector are separate containers, so modifying one afterward does not modify the other.
1. Convert a C++ array to vector using the vector range constructor
The vector range constructor accepts two iterators or pointers: the first element to copy and the position just after the last element to copy. For a built-in array, begin(array) and end(array) provide those boundaries.
main.cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
int numsArr[] = {2, 5, 1, 8, 4, 3, 6};
vector<int> nums(begin(numsArr), end(numsArr));
for (int i = 0; i < nums.size(); i++) {
cout << nums[i] << endl;
}
}
Output
2
5
1
8
4
3
6
The constructor copies all seven values from numsArr into nums. In code that uses the qualified names std::begin() and std::end(), include <iterator> explicitly.
2. Convert part of a C++ array to vector with a half-open range
You do not have to copy the whole array. Pass pointers to the first element you want and to the position immediately after the last element you want.
In the following example, the source array has seven elements. The vector is constructed from array indexes 2, 3, and 4. The second boundary is index 5, which is not included.
main.cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
int numsArr[] = {2, 5, 1, 8, 4, 3, 6};
vector<int> nums(begin(numsArr)+2, begin(numsArr)+5);
for (int i = 0; i < nums.size(); i++) {
cout << nums[i] << endl;
}
}
Output
1
8
4
Only the elements in the half-open range [2, 5) are copied. In C++ range notation, the starting position is included and the ending position is excluded.
3. Convert an array to an existing vector using assign()
If the vector has already been declared, assign() can replace its current contents with values from the array. Like the range constructor, it accepts a beginning and ending range.
Syntax
vectorName.assign(std::begin(arrayName), std::end(arrayName));
Example:
#include <iostream>
#include <iterator>
#include <vector>
int main() {
int numsArr[] = {10, 20, 30, 40};
std::vector<int> nums;
nums.assign(std::begin(numsArr), std::end(numsArr));
for (int value : nums) {
std::cout << value << '\n';
}
}
Output
10
20
30
40
Use the range constructor when you are creating the vector at the same time as the conversion. Use assign() when the vector already exists and you want to replace its contents.
4. Append array elements to an existing vector using insert()
assign() replaces the current vector contents. If you want to keep the existing elements and append the array values instead, use the range form of insert().
#include <iostream>
#include <iterator>
#include <vector>
int main() {
int numsArr[] = {30, 40};
std::vector<int> nums = {10, 20};
nums.insert(nums.end(), std::begin(numsArr), std::end(numsArr));
for (int value : nums) {
std::cout << value << '\n';
}
}
Output
10
20
30
40
The first argument, nums.end(), tells insert() to place the copied array range at the end of the vector.
5. Convert a C++ array to vector using a loop and push_back()
You can also copy the array elements one by one. This approach is useful when each element must be checked, transformed, or conditionally added while copying.
In the following example, we shall use C++ For Loop, to add elements of array to vector using push_back().
main.cpp
#include <iostream>
#include <vector>
using namespace std;
int main() {
int numsArr[] = {2, 5, 1, 8, 4, 3, 6};
int len = end(numsArr) - begin(numsArr);
vector<int> nums;
for (int i=0; i < len; i++) {
nums.push_back(numsArr[i]);
}
for (int element : nums) {
cout << element << endl;
}
}
Output
2
5
1
8
4
3
6
If you know how many elements will be inserted, you can call reserve() before a push_back() loop to reserve enough capacity and reduce vector reallocations. Reserving capacity does not add elements and does not change size().
6. Convert an array to vector when you have a pointer and element count
Sometimes the array is available through a pointer together with a known element count. In that case, construct the vector from the pointer to the first element and the pointer one past the last element.
std::vector<int> values(ptr, ptr + count);
The count must correctly describe the valid range. A pointer by itself does not contain the array length.
7. Convert std::array to std::vector using begin() and end()
The same range-constructor idea also works with std::array. Pass its begin() and end() iterators to the vector constructor.
#include <array>
#include <vector>
int main() {
std::array<int, 4> numsArr = {3, 6, 9, 12};
std::vector<int> nums(numsArr.begin(), numsArr.end());
}
This is useful when the source is a fixed-size std::array but the destination needs vector operations such as dynamic resizing and insertion.
Why resize() Is Not Required When Converting an Array to a Vector
You do not need to call resize() before using the range constructor, assign(), or insert(). These operations set or grow the vector size as needed. A push_back() loop also increases the vector size one element at a time.
Be careful not to call resize(count) on an empty vector and then use push_back() for the same count array elements. resize(count) already creates count elements; the loop would then append additional elements. If the goal is only to reduce reallocations before repeated push_back() calls, use reserve(count) instead.
C++ Array to Vector: Choosing the Right Method
- Range constructor: use it for the shortest and clearest conversion when creating a new vector from an array.
- Partial range constructor: use it when only a selected contiguous part of the array should be copied.
- assign(): use it when a vector already exists and its contents should be replaced by the array values.
- insert(): use it when the array values should be added while keeping the vector's existing elements.
- Loop with push_back(): use it when you need filtering, conversion, or other per-element logic while copying.
Each of these methods copies the selected array elements into vector storage. For a straightforward whole-array conversion, the range constructor is usually the simplest choice.
Key Points About Converting Arrays to Vectors in C++
- The vector constructor uses a half-open range: the first boundary is included and the second is excluded.
std::begin(array)andstd::end(array)are convenient for built-in arrays whose size is known at compile time.- When using a raw pointer, you also need the number of valid elements or another correct end pointer.
- The vector contains copies of the source elements; it does not become a view of the original array.
- For an existing vector,
assign()is a direct way to replace its contents from an array range.
In this C++ Tutorial, we learned how to convert an array to a vector using the vector range constructor, a selected array range, assign(), insert(), and a loop with push_back().
TutorialKart.com