C++ std::list::pop_back
The std::list::pop_back
function removes the last element from a std::list
. This operation reduces the size of the list by one but does not return the removed element. The function is commonly used when managing elements in LIFO (Last In, First Out) operations.
Syntax of std::list::pop_back
void pop_back();
Parameters
This function does not accept any parameters.
Return Value
This function does not return a value. It modifies the list by removing the last element.
Exceptions
The std::list::pop_back
function does not throw exceptions itself. However, calling it on an empty list results in undefined behavior. To avoid this, always ensure the list is not empty before calling pop_back
. This can be done using the empty()
method.
Examples for std::list::pop_back
Example 1: Removing Elements from the End of a List
This example demonstrates how to use pop_back
to remove elements from the end of a list:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> myList = {10, 20, 30, 40};
myList.pop_back(); // Remove the last element (40)
std::cout << "List contents after pop_back: ";
for (const auto& elem : myList) {
std::cout << elem << " ";
}
std::cout << std::endl;
return 0;
}
Explanation:
- A
std::list
namedmyList
is initialized with the elements{10, 20, 30, 40}
. - The
pop_back
function is called, removing the last element40
. - The remaining elements of the list are printed using a range-based
for
loop.
Output:
List contents after pop_back: 10 20 30
Example 2: Handling Empty List
This example demonstrates how to safely call pop_back
on a list to avoid undefined behavior:
Program
#include <iostream>
#include <list>
int main() {
std::list<int> myList;
if (!myList.empty()) {
myList.pop_back();
} else {
std::cout << "List is empty, cannot call pop_back." << std::endl;
}
return 0;
}
Explanation:
- A
std::list
namedmyList
is created and is initially empty. - The
empty()
method is used to check if the list contains elements before callingpop_back
. - If the list is empty, a message is printed to indicate that the operation cannot be performed.
Output:
List is empty, cannot call pop_back.
Example 3: Removing Complex Objects
This example demonstrates how to use pop_back
with a list of complex objects:
Program
#include <iostream>
#include <list>
#include <string>
struct Person {
std::string name;
int age;
Person(const std::string& name, int age) : name(name), age(age) {}
};
int main() {
std::list<Person> people;
people.push_back(Person("Arjun", 30));
people.push_back(Person("Ram", 25));
people.pop_back(); // Remove the last element ("Bob")
for (const auto& person : people) {
std::cout << person.name << " (" << person.age << " years old)" << std::endl;
}
return 0;
}
Explanation:
- A
Person
struct is defined with two members:name
(a string) andage
(an integer). - A
std::list
ofPerson
objects namedpeople
is created, and two objects (“Arjun” and “Ram”) are added usingpush_back
. - The
pop_back
function is called, removing the last object (“Ram”). - A range-based
for
loop iterates through the list and prints the details of the remainingPerson
objects.
Output:
Arjun (30 years old)