YouTip LogoYouTip

Cpp Libs List Pop_Back

## C++ `std::list::pop_back` Member Function The `pop_back` function is a built-in member function of the C++ Standard Template Library (STL) `std::list` container. It is used to **remove the last element** from the list, effectively reducing the container size by one. Because `std::list` is implemented as a doubly-linked list, removing the last element is highly efficient and does not require shifting any other elements in memory. --- ## Syntax and Parameters ### Function Prototype ```cpp void pop_back(); ``` ### Parameters * **None**: The function does not accept any arguments. ### Return Value * **`void`**: It does not return any value. If you need to access the value of the last element before removing it, you must call `back()` first. ### Complexity * **Time Complexity**: $\mathcal{O}(1)$ (Constant time). * **Space Complexity**: $\mathcal{O}(1)$. --- ## Behavior and Safety Considerations * **Size Reduction**: Calling `pop_back()` decreases the container's `size()` by exactly `1`. * **Iterator Validity**: Only iterators, pointers, and references pointing to the removed element are invalidated. All other iterators remain valid. * **Undefined Behavior**: Calling `pop_back()` on an **empty list** results in **undefined behavior**. You must always ensure the list is not empty before invoking this function. --- ## Code Examples ### Example 1: Basic Usage - Removing the Last Element This example demonstrates how to use `pop_back()` to sequentially remove elements from the end of a list. ```cpp #include #include int main() { // Initialize a list with 5 elements std::list numbers = {10, 20, 30, 40, 50}; std::cout << "Original list: "; for(int n : numbers) std::cout << n << " "; std::cout << std::endl; // Remove the last element (50) numbers.pop_back(); std::cout << "After pop_back(): "; for(int n : numbers) std::cout << n << " "; std::cout << std::endl; // Remove the next last element (40) numbers.pop_back(); std::cout << "After another pop_back(): "; for(int n : numbers) std::cout << n << " "; std::cout << std::endl; return 0; } ``` #### Expected Output: ```text Original list: 10 20 30 40 50 After pop_back(): 10 20 30 40 After another pop_back(): 10 20 30 ``` --- ### Example 2: Safe Usage of `pop_back` To prevent undefined behavior, check if the list is empty using the `.empty()` member function before calling `pop_back()`. ```cpp #include #include void safePopBack(std::list& lst) { // Check if the list contains elements before popping if(!lst.empty()) { lst.pop_back(); std::cout << "Successfully removed the last element." << std::endl; } else { std::cout << "The list is empty. Cannot perform pop_back()." << std::endl; } } int main() { std::list data = {1, 2, 3}; safePopBack(data); // Removes 3 safePopBack(data); // Removes 2 safePopBack(data); // Removes 1 safePopBack(data); // List is now empty; handles gracefully return 0; } ``` #### Expected Output: ```text Successfully removed the last element. Successfully removed the last element. Successfully removed the last element. The list is empty. Cannot perform pop_back(). ``` --- ## Summary Table | Feature | Description | | :--- | :--- | | **Header Required** | `#include ` | | **Operation** | Removes the last element of the list | | **Time Complexity** | $\mathcal{O}(1)$ | | **Return Value** | `void` | | **Precondition** | The list must not be empty (`!lst.empty()`) |
← Cpp Libs List EraseCpp Libs Array Swap Begin End β†’