C++ std::list::front

The std::list::front function provides access to the first element of a std::list. This function returns a reference to the first element, allowing it to be read or modified.


Syntax of std::list::front

</>
Copy
reference front();
const_reference front() const;

Parameters

The std::list::front function does not take any parameters.

Return Value

Returns a reference to the first element in the list. If the list is const, the returned reference is also const, meaning the element cannot be modified.

Exceptions

The std::list::front function does not throw exceptions, but calling it on an empty list results in undefined behavior. It is recommended to check whether the list is empty using empty() before calling this function.


Examples for std::list::front

Example 1: Accessing the First Element Using std::list::front

This example demonstrates how to use front() to access the first element of a list:

</>
Copy
#include <iostream>
#include <list>

int main() {
    std::list<int> myList = {10, 20, 30};

    std::cout << "The first element is: " << myList.front() << std::endl;

    return 0;
}

Explanation:

  1. Define a list: A std::list named myList is initialized with elements {10, 20, 30}.
  2. Access the first element: The front() function is called on myList to retrieve the first element, which is 10.
  3. Print the element: The retrieved element is printed to the console.

Output:

The first element is: 10

Example 2: Modifying the First Element Using std::list::front

This example demonstrates how to modify the first element of a list using front():

</>
Copy
#include <iostream>
#include <list>

int main() {
    std::list<int> myList = {1, 2, 3};

    std::cout << "Before modification, first element: " << myList.front() << std::endl;

    myList.front() = 100; // Modify the first element

    std::cout << "After modification, first element: " << myList.front() << std::endl;

    return 0;
}

Explanation:

  1. Define a list: A std::list named myList is initialized with elements {1, 2, 3}.
  2. Access the first element: The front() function is called to retrieve the first element, which is 1.
  3. Modify the first element: The value of the first element is updated to 100 using myList.front() = 100.
  4. Print the modified element: The updated first element is printed to the console.

Output:

Before modification, first element: 1
After modification, first element: 100

Exception Handling in std::list::front

The std::list::front function does not throw exceptions. However, calling this function on an empty list results in undefined behavior. It is recommended to use the empty() function to check whether the list is empty before calling front().

Example 3: Avoiding Undefined Behavior with std::list::front

</>
Copy
#include <iostream>
#include <list>

int main() {
    std::list<int> emptyList;

    if (!emptyList.empty()) {
        std::cout << "First element: " << emptyList.front() << std::endl;
    } else {
        std::cout << "The list is empty. No elements to access." << std::endl;
    }

    return 0;
}

Output:

The list is empty. No elements to access.