C++ <array> size function

C++ container class <array>


sizeIt is one of the most commonly used and fundamental functions in array, used toGet the number of elements in the container。

sizeis a member function of container classes, used toreturn the number of elements currently contained in the container. Unlike vector, the size of array is fixed and determined at compile time.

sizeIt is the most frequently used function when operating on array, used in scenarios such as loop traversal and boundary checking.

Word Definitions: sizemeans "size", indicating the number of elements in the container.


Basic syntax and parameters

sizeIt is a member function of the container class, and calling it requires no arguments.

Syntax format

constexpr size_type size() const noexcept;

Parameter description

  • Parameter: No parameters
    • sizetakes no arguments.

Function description

  • Return Value: returnssize_typeType (usuallysize_t), representing the number of elements in the container.
  • Effect: Returns a non-negative integer representing the number of elements fixedly stored in the container.
  • Note: size()It returns a compile-time constant (constexpr), which can be used at compile time.

Example

Let us thoroughly master through a series of examplessizeUsage.

Example 1: Basic usage - getting the number of elements

Example

#include <iostream>
#include <array>

int main() {
    // arrays of different sizes
    std::array<int, 3> small = {1, 2, 3};
    std::array<int, 5> medium = {1, 2, 3, 4, 5};
    std::array<int, 10> large = {};

    std::cout << "small array size: " << small.size() << std::endl;
    std::cout << "medium array size: " << medium.size() << std::endl;
    std::cout << "large array size: " << large.size() << std::endl;

    return 0;
}

Expected output:

small array 大小: 3
medium array 大小: 5
large array 大小: 10

Code analysis:

  1. The size of array is determined at compile time and specified by the template parameter.
  2. size()What is returned is this compile-time-determined value.

Example 2: Using size() in a loop

size()Most commonly used to control loop iteration.

Example

#include <iostream>
#include <array>

int main() {
    std::array<int, 5> numbers = {10, 20, 30, 40, 50};

    // Use size() to control the loop
    std::cout << "Iterate over all elements: ";
    for(size_t i = 0; i < numbers.size(); ++i) {
        std::cout << numbers[i] << " ";
    }
    std::cout << std::endl;

    // Use size() to get the boundary for reverse traversal
    std::cout << "Reverse traversal: ";
    for(size_t i = numbers.size(); i > 0; --i) {
        std::cout << numbers[i - 1] << " ";
    }
    std::cout << std::endl;

    return 0;
}

Expected output:

遍历所有元素: 10 20 30 40 50
反向遍历: 50 40 30 20 10

Code analysis:

  • numbers.size()returns 5, the loop conditioni < numbers.size()Ensure it does not go out of bounds.
  • When traversing in reverse, fromsize()starts decrementing to 1, then accesses[i-1]。
  • Example 3: Using constexpr size

    array'ssize()It is constexpr and can be used at compile time.

    Example

    #include <iostream>
    #include <array>

    // Template function: create an array of a given size
    template<size_t N>
    void printSize(const std::array<int, N>& arr) {
        // size() can be used where compile-time constants are needed
        constexpr size_t s = arr.size();
        std::cout << "Array size: " << s << std::endl;
    }

    int main() {
        std::array<int, 5> arr = {1, 2, 3, 4, 5};
        printSize(arr);

        return 0;
    }

    Code analysis:

    • array'ssize()Yesconstexpr, can be evaluated at compile time.
    • This is different from vector, where vector'ssize()is not a compile-time constant.

    C++ container class <array>

    other extensions