5 minute read

Introduction

In this article, we will talk about how to determine the length of an array in C++. First off, let’s define what the length of an array is. Consider the following scenario:

int myArray[] = {1, 2, 3, 4, 5};

In the example above, we have 5 elements so the length of the array will be 5. Therefore, we refer to the total number of elements in the array.

3 ways to find the Length of an Array in C++

In C++, there are several methods to determine the length of an array (in other words, the number of elements in an array).

These methods include:

  1. Using the sizeof() operator
  2. Iterating through the array using a for loop
  3. Using the std::size() function.

In this article, we will explore each of these methods in detail and provide examples of how to use them.

Method 1: Using the sizeof() operator

One easy way is to use the built-in C sizeof() operator. Next, we will explain what the operator does in detail.

What does sizeof() do?

The sizeof() operator in C++ returns the size of a variable in bytes. It can be used on any variable or data type.

Next, we will need divide the total size in bytes by the any element of the array, thus getting the amount of elements that we have in our array.

Let’s see an in-depth example of the `sizeof()’ operator to determine the length of an array in C++.

Example with sizeof()

#include <iostream>

int main()
{
    int myArray[] = {1, 2, 3, 4, 5};
    std::cout << "Size of array: " << sizeof(myArray) << std::endl; // Output: 20 bytes
    std::cout << "Size of each element: " << sizeof(myArray[0]) << std::endl; // Output: 4 bytes
    std::cout << "Total elements: " << sizeof(myArray) / sizeof(myArray[0]) << std::endl; // Output: 5 elements
    return 0;
}

Output:

Size of array: 20
Size of each element: 4
Total elements: 5

Notice how the original array is 20 bytes long and each int is 4 bytes long. Therefore, 20 bytes / 4 bytes = 5 elements. Thus, we finally we get the length of our array.

Method 2: Iterating through array

We can also iterate through an array to count how many elements we have and thus, determining the length of an array. In order to do this, we will use a for loop and a count variable where we will store the length.

Let’s see a deeper example of this method in action.

Example using the loop method

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int count = 0;

    for(auto i: arr)
    {
        count++;
    }

    std::cout << "The number of elements in the array is: " << count << std::endl;
    return 0;
}

Output:

The number of elements in the array is: 5

Notice how we are using a range-based for loop when typing for(auto i: arr). It is important to keep in mind that these were introduced in C++11.

Therefore, if you use an older C++ version, you may want to consider other methods, so keep reading below.

Method 3: Using size() function

Additionally, the size() function from the STD library can be used in order to determine the length of an array in C++.

However, keep in mind that this method will only work for <vector> or <array> containers.

In other words, if we declare:

int arr[] = {1, 2, 3, 4, 5};
int count = arr.size();

We would get the following error:

error: request for member ‘size’ in ‘arr’, which is of non-class type ‘int [5]’

What does size() do?

The std::size( ) function is a built-in function in C++ STL (Standard Template Library). The std::size( ) function returns the length of a variable or container.

It is available for the following headers: <array>, <set>, <list>, <map>, <string>, <vector>, among many others.

Therefore, if we are using an <array> container, then we might want to use std::size() for that scenario.

Let us see an example of std::size() below:

Example using std::size() function

#include <iostream>
#include <array> //Add this

int main() {
    std::array<int, 5> arr = {1, 2, 3, 4, 5};
    std::cout << "The number of elements in the array is: " << arr.size() << std::endl;
    return 0;
}

Output:

The number of elements in the array is: 5

As mentioned, this will not work for normal C-like arrays but it is a good choice to keep in mind for C++ <array> containers.

Is there a length() function for C++ arrays?

No, the string::length() function is only available for the <string> data type. It only returns the length of the string in bytes.

It is not necessarily equal to its storage capacity.

Example of the length() function with a string

#include <iostream>
#include <string>
int main ()
{
  std::string str ("Hello from ProgramSquared");
  std::cout << "The size of str is " << str.length() << " bytes.\n";
  return 0;
}

Output:

The size of str is 25 bytes.

Notice how each char in our string is 1 byte long, which is why it is 25 bytes long. Note that white-spaces are also counted as characters.

References

Read more about the built-in sizeof() operator in this link: https://en.cppreference.com/w/cpp/language/sizeof.

Read more about the new range-based for loop here: https://en.cppreference.com/w/cpp/language/range-for.

Read more about the STL function std::size: https://en.cppreference.com/w/cpp/iterator/size.

Read more about the String member function std::length: https://cplusplus.com/reference/string/string/length/.