To get the length of an array in C++, you can use the size
function from the array
library.
In this example,
header file for array operations.numbers
with some values.size
function to get the length of the array and store the returned value in length
variable.length
to standard output.#include <iostream>
#include <array>
int main() {
std::array<int, 5> numbers = {1, 2, 3, 4, 5};
int length = numbers.size();
std::cout << "Length of array is: " << length;
return 0;
}
Length of array is: 5
In this example,
header file for array operations.names
with some strings.size
function to get the length of the array and store the returned value in length
variable.length
to standard output.#include <iostream>
#include <array>
#include <string>
int main() {
std::array<std::string, 3> names = {"Alice", "Bob", "Charlie"};
int length = names.size();
std::cout << "Length of array is: " << length;
return 0;
}
Length of array is: 3
In this tutorial, we learned How to Get Array Length in C++ language with well detailed examples.