C++ std::list::push_back

The std::list::push_back function adds a new element to the end of a std::list. The new element is either copied or moved into the list. This operation is efficient because it does not require reallocation of elements, making it suitable for dynamically growing the list.


Syntax of std::list::push_back

</>
Copy
void push_back(const value_type& val);
void push_back(value_type&& val);

Parameters

ParameterDescription
valThe value of the element to be added to the end of the list. It can be passed as a constant reference or an rvalue.

Return Value

This function does not return a value. It modifies the list by adding a new element at the end.

Exceptions

The std::list::push_back function may throw exceptions in the following cases:

  • If memory allocation fails.
  • If the copy or move constructor of the element being added throws an exception.

The function provides strong exception safety. If an exception occurs, the state of the list remains unchanged.


Examples for std::list::push_back

Example 1: Adding Elements to the End of a List

This example demonstrates how to use push_back to add elements to the end of a list:

Program

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

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

    myList.push_back(10); // Add 10 to the end of the list
    myList.push_back(20); // Add 20 to the end of the list
    myList.push_back(30); // Add 30 to the end of the list

    std::cout << "List contents: ";
    for (const auto& elem : myList) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

Explanation:

  1. A std::list named myList is created and is initially empty.
  2. The push_back function is used to add three elements (10, 20, and 30) to the end of the list.
  3. A range-based for loop is used to iterate through the list and print its contents.

Output:

List contents: 10 20 30

Example 2: Adding Complex Objects Using push_back

This example demonstrates how to use push_back to add complex objects to a list:

Program

</>
Copy
#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;

    // Add Person objects to the list
    people.push_back(Person("Alice", 30));
    people.push_back(Person("Bob", 25));

    for (const auto& person : people) {
        std::cout << person.name << " (" << person.age << " years old)" << std::endl;
    }

    return 0;
}

Explanation:

  1. The Person struct is defined with two members: name (a string) and age (an integer).
  2. A std::list of Person objects named people is created.
  3. The push_back function is used to add two Person objects (“Alice” and “Bob”) to the end of the list.
  4. A range-based for loop iterates through the list and prints the details of each Person.

Output:

Alice (30 years old)
Bob (25 years old)

Example 3: Exception Safety

This example demonstrates how push_back handles exceptions when adding an object:

Program

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

struct Faulty {
    int value;

    Faulty(int v) {
        if (v < 0) {
            throw std::invalid_argument("Negative value not allowed");
        }
        value = v;
    }
};

int main() {
    std::list<Faulty> myList;

    try {
        myList.push_back(Faulty(10)); // Success
        myList.push_back(Faulty(-1)); // Throws an exception
    } catch (const std::exception& e) {
        std::cout << "Exception: " << e.what() << std::endl;
    }

    std::cout << "List contents: ";
    for (const auto& elem : myList) {
        std::cout << elem.value << " ";
    }
    std::cout << std::endl;

    return 0;
}

Explanation:

  1. The Faulty struct has a constructor that throws an exception if a negative value is passed.
  2. The push_back function is used to add a valid object (10), which succeeds.
  3. An attempt to add an invalid object (-1) throws an exception. The exception is caught and handled gracefully.
  4. After the exception, the state of the list remains unchanged, demonstrating the strong exception safety of push_back.

Output:

Exception: Negative value not allowed
List contents: 10