HOWTO · C++
How to Convert Vector to Array in C++
This article introduces how to convert a vector to an array in C++.
On this page
A std::vector stores its elements contiguously, so APIs that accept a pointer and an element count can use its storage directly. This gives array-style access, but it does not create a separate array: the pointer refers to elements owned by the vector. If the receiving code needs independent storage, copy the elements into a destination whose size is sufficient.
Use data() for a temporary pointer view, std::copy for an independent copy, and std::transform when the copied values should change. Keep the vector alive while a pointer into its elements is in use. Operations that reallocate the vector make pointers into its storage unusable.
Use data() to Access a Vector’s Elements as an Array
The data() member returns a pointer to the vector’s contiguous element storage. Pair it with size() when passing the data to an API that accepts a pointer and a count. The pointer aliases the vector: writing through it changes the corresponding vector element, and it must not outlive the vector or an operation that reallocates its storage.
Run this example:
#include <cstddef>
#include <iostream>
#include <vector>
int main() {
std::vector<int> values{1, 2, 3};
int* elements = values.data();
elements[1] = 20;
for (std::size_t i = 0; i < values.size(); ++i) {
if (i != 0) std::cout << ' ';
std::cout << elements[i];
}
std::cout << '\n';
}
1 20 3
For an empty-capable vector, data() can be obtained, but do not dereference the returned pointer unless an element exists. Always use the vector’s size as the bound when indexing through the pointer.
Use the Address-Of Operator with a Nonempty Vector
For a vector known to contain at least one element, &values[0] points to its first element and can be indexed across the vector’s elements. It has the same ownership and lifetime as a pointer obtained with data(). The precondition matters: indexing element zero when the vector is empty is invalid. Prefer data() when the vector may be empty, and still check the size before dereferencing or passing a pointer to code that expects at least one element.
#include <cstddef>
#include <iostream>
#include <vector>
int main() {
std::vector<int> values{4, 5, 6};
int* elements = &values[0];
for (std::size_t i = 0; i < values.size(); ++i) {
if (i != 0) std::cout << ' ';
std::cout << elements[i];
}
std::cout << '\n';
}
4 5 6
Copy Vector Elements into an Array with std::copy()
std::copy writes each source element to an already-created destination range; it does not allocate that range. When the element count is known at compile time, std::array provides a real, fixed-size array object. Its size and element type must accommodate the source. For a count known only at runtime, use suitable dynamic storage such as a destination std::vector or an owned dynamic array if the receiving API specifically requires one.
#include <algorithm>
#include <array>
#include <cstddef>
#include <iostream>
#include <vector>
int main() {
std::vector<int> values{7, 8, 9};
std::array<int, 3> result{};
std::copy(values.begin(), values.end(), result.begin());
for (std::size_t i = 0; i < result.size(); ++i) {
if (i != 0) std::cout << ' ';
std::cout << result[i];
}
std::cout << '\n';
}
7 8 9
The destination is independent of the source, so changing result does not change values. Ensure the destination has room for every source element before calling std::copy; the algorithm does not check destination capacity. A variable-length built-in array is not standard C++, because its bound must be a compile-time constant.
Transform Vector Elements While Copying with std::transform()
Use std::transform when copying should also compute new values. Its output range must have room for one result per input element, just as with std::copy. This example doubles each integer while storing it in an independent std::array.
#include <algorithm>
#include <array>
#include <cstddef>
#include <iostream>
#include <vector>
int main() {
std::vector<int> values{2, 3, 4};
std::array<int, 3> doubled{};
std::transform(values.begin(), values.end(), doubled.begin(),
[](int value) { return value * 2; });
for (std::size_t i = 0; i < doubled.size(); ++i) {
if (i != 0) std::cout << ' ';
std::cout << doubled[i];
}
std::cout << '\n';
}
4 6 8
For an unchanged copy, std::copy states the intent more directly. Select a destination type that can store the transformed results and size it for the full input range.
Copy Vector Elements with a for Loop
A loop offers explicit control over each assignment. The same destination-size requirement applies: every index used for the source must be valid in the destination. With a compile-time-sized destination, check that its extent matches the vector before copying; for runtime-sized input, use an appropriately sized dynamic destination.
#include <array>
#include <cstddef>
#include <iostream>
#include <vector>
int main() {
std::vector<int> values{10, 11, 12};
std::array<int, 3> result{};
for (std::size_t i = 0; i < values.size(); ++i) {
result[i] = values[i];
}
for (std::size_t i = 0; i < result.size(); ++i) {
if (i != 0) std::cout << ' ';
std::cout << result[i];
}
std::cout << '\n';
}
10 11 12
Choose data() when an API needs a temporary view of vector-owned elements, and keep the vector stable and alive for the duration of that use. Use std::copy when separate storage is required, std::transform when values need to change during that copy, and a loop when per-element control is useful. A fixed-size std::array suits a compile-time-known count; otherwise, use a destination with runtime sizing when the API allows it.