HOWTO · C++
How to Find Length of an Array in C++
This article shows five ways to count array elements in C++: a range-based for loop, sizeof division for a built-in array, the size() members of std::array and std::vector, std::end(array) - std::begin(array), and the std::size free function.
On this page
To find an array’s length in C++, count its elements. The right technique depends on the type: use sizeof for a built-in array in the scope where it is declared, size() for std::array or std::vector, or std::size for a generic standard-library call. A range-based for loop can count the elements in a range you can already iterate.
Use a Range-Based for Loop to Count Array Elements in C++
A range-based for loop visits each element in an array or other iterable range. Increment a counter once per visit to count the elements:
#include <iostream>
int main() {
int values[] = {10, 20, 30, 40, 50};
int count = 0;
for (int value : values) {
(void)value;
++count;
}
std::cout << "Element count: " << count << '\n';
}
This counts the range that values provides. It does not discover the allocation size of a raw pointer or dynamically allocated storage; in those cases, keep the element count separately or use a container that reports its size.
Use sizeof to Find the Length of a Built-In Array
For a built-in array that is still an array in the current scope, divide its total byte size by the byte size of one element:
#include <iostream>
#include <cstddef>
int main() {
int values[] = {1, 2, 3, 4, 5};
const std::size_t count = sizeof(values) / sizeof(values[0]);
std::cout << "Element count: " << count << '\n';
}
sizeof(values) gives the number of bytes occupied by the whole array, while sizeof(values[0]) gives the bytes occupied by one element. Their quotient is the element count. The example includes <cstddef> for std::size_t.
This calculation stops working as an element-count calculation after an array is passed to a function. A parameter written as int values[] is adjusted to int* values; inside that function, sizeof(values) measures the pointer, not the original array. The resulting quotient depends on the implementation’s pointer and element sizes, so it is not a portable count. Pass the count separately or accept a container instead.
Use size() with std::array and std::vector
Both std::array and std::vector provide a size() member function that returns the number of elements. std::array represents a fixed-size array, while a vector can change size as elements are added or removed.
#include <array>
#include <iostream>
#include <vector>
int main() {
std::array<int, 3> fixed{4, 8, 12};
std::vector<int> resizable{5, 10, 15, 20};
std::cout << "std::array: " << fixed.size() << '\n';
std::cout << "std::vector: " << resizable.size() << '\n';
}
Use the member belonging to the container you have. std::array::size() reports its fixed element count; std::vector::size() reports the vector’s current number of elements.
Use std::end() and std::begin() to Find Array Length
For a built-in array, std::begin points to its first element and std::end points one position past its last element. Subtracting the two pointers gives the number of elements between them. Include <iterator> for these functions.
#include <iostream>
#include <iterator>
int main() {
int values[] = {2, 4, 6, 8};
const auto count = std::end(values) - std::begin(values);
std::cout << "Element count: " << count << '\n';
}
The subtraction is valid because both pointers refer into the same array (or to its one-past-the-end position). This is useful when working with a built-in array; for std::array or std::vector, the size() member is usually clearer.
Use std::size() to Get the Number of Elements
std::size is a standard-library function that returns an element count. It works with built-in arrays and standard containers such as std::array and std::vector. Include <iterator> to use it.
#include <array>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
int built_in[] = {3, 6, 9};
std::array<int, 2> fixed{7, 14};
std::vector<int> resizable{11, 22, 33, 44};
std::cout << "Built-in array: " << std::size(built_in) << '\n';
std::cout << "std::array: " << std::size(fixed) << '\n';
std::cout << "std::vector: " << std::size(resizable) << '\n';
}
Unlike the size() member, std::size(container) uses a free function call. It provides one spelling for the supported array and container types, while each container’s own size() member remains available.
Choose the Right Method for Your Array Type
- For a built-in array in the same scope where it is declared, use
sizeof(array) / sizeof(array[0])orstd::size(array). - For
std::arrayandstd::vector, prefer theirsize()member. Usestd::size(container)if a common function call is useful. - Use
std::end(array) - std::begin(array)when iterator subtraction suits the surrounding code. - Use a range-based
forloop when you want to count an iterable range by visiting its elements. - For a pointer or dynamically allocated array, retain the count separately or use a container. The pointer alone does not carry the allocation’s element count.
The key distinction is between storage size in bytes and the number of elements. sizeof measures bytes; divide by one element’s byte size only while the expression still refers to the complete built-in array. Container size functions and iterator subtraction report element counts directly.