C++ <array> back function

C++ container class <array>


In various operations on array,backis a very practical function, used forgetting the last element。

backis a member function of container classes, used forreturns the last element of the container. It is equivalent toat(size()-1)oroperator[](size()-1), but with clearer semantics.

backprovides an intuitive way to access the last element of a container.

Word Definitions: backmeans "back", i.e., get the last (rearmost) element.


Basic syntax and parameters

backis a member function of the container class, and calling it is very straightforward, requiring no parameters.

Syntax format

reference back();
const_reference back() const;

Parameter description

  • Parameter: No parameters
    • backdoes not accept any parameters.

Function description

  • Return Value: returns the last element of the container as aReferenceIf the container is a constant container, it returns a constant reference.
  • Effect: returns the last element of the container (indexsize()-1element at position).
  • Note: array cannot be empty, so callingbackIt is safe.

Example

Let's thoroughly master through a series of examples.backUsage.

Example 1: Basic usage - getting the last element

Example

#include <iostream>
#include <array>

int main() {
    // 1. Create an array and initialize it
    std::array<int, 5> numbers = {10, 20, 30, 40, 50};

    std::cout << "The size of array is: " << numbers.size() << std::endl;

    // 2. Use back() to get the last element
    std::cout << "The last element (back): " << numbers.back() << std::endl;
    std::cout << "Using at(size()-1): " << numbers.at(numbers.size() - 1) << std::endl;
    std::cout << "Using [size()-1]: " << numbers[numbers.size() - 1] << std::endl;

    return 0;
}

Expected output:

array的大小是: 5
最后一个元素 (back): 50
使用 at(size()-1): 50
使用 [size()-1]: 50

Code analysis:

  1. numbers.back()returns the last element50。
  2. It andnumbers.at(numbers.size()-1)andnumbers[numbers.size()-1]returns the same value, but with more explicit semantics.

Example 2: Modifying the value of the last element

backIt returns a reference, so it can be used to modify the value of the element.

Example

#include <iostream>
#include <array>
#include <string>

int main() {
    std::array<std::string, 3> tasks = {"Learn C++", "Do homework", "Read documentation"};

    std::cout << "Before modification, the last task: " << tasks.back() << std::endl;

    // Use back() to get a reference and modify the element
    tasks.back() = "Complete project";

    std::cout << "After modification, the last task: " << tasks.back() << std::endl;

    return 0;
}

Expected output:

Before modification, the last task: Read the documentation
After modification, the last task: Complete the project

Code analysis:

  • tasks.back() = "完成项目";The value of the last element is modified through the reference.
  • back()returns a modifiable lvalue reference.

Example 3: Using back with sort to get max/min values

After sorting,back()Returns the maximum value.

Example

#include <iostream>
#include <array>
#include <algorithm>

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

    std::cout << "Before sorting: ";
    for(int n : numbers) std::cout << n << " ";
    std::cout << std::endl;

    // Sort
    std::sort(numbers.begin(), numbers.end());

    std::cout << "After sorting: ";
    for(int n : numbers) std::cout << n << " ";
    std::cout << std::endl;

    std::cout << Minimum value (front): << numbers.front() << std::endl;
    std::cout << Maximum value (back): << numbers.back() << std::endl;

    return 0;
}

Expected output:

排序前: 30 10 50 20 40
排序后: 10 20 30 40 50
最小值 (front): 10
最大值 (back): 50

Code analysis:

  • After sorting,front()Returns the minimum value,back()Returns the maximum value.
  • This is a simple way to get the maximum/minimum value.

C++ container class <array>

other extensions