C++ std::list::reverse

The std::list::reverse function reverses the order of the elements in a std::list. It modifies the list in place without creating a new list or allocating additional memory. This operation is efficient for doubly-linked lists, as it simply swaps the links between elements.


Syntax of std::list::reverse

</>
Copy
void reverse();

Parameters

This function does not accept any parameters.

Return Value

This function does not return a value. It modifies the list in place by reversing the order of its elements.

Exceptions

The std::list::reverse function does not throw exceptions. It provides a no-throw guarantee since it only modifies the internal links between elements.


Examples for std::list::reverse

Example 1: Reversing a List of Integers

This example demonstrates how to use reverse to reverse the elements of a list of integers:

Program

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

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

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

    // Reverse the list
    myList.reverse();

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

    return 0;
}

Explanation:

  1. A std::list named myList is initialized with {10, 20, 30, 40}.
  2. The reverse function is called, which reverses the order of the elements in myList.
  3. The modified list contents are printed, showing the reversed order {40, 30, 20, 10}.

Output:

List contents before reverse: 10 20 30 40
List contents after reverse: 40 30 20 10

Example 2: Reversing a List of Strings

This example demonstrates how to reverse the order of a list containing strings:

Program

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

int main() {
    std::list<std::string> myList = {"apple", "banana", "cherry", "date"};

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

    // Reverse the list
    myList.reverse();

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

    return 0;
}

Explanation:

  1. A std::list of strings named myList is initialized with {"apple", "banana", "cherry", "date"}.
  2. The reverse function is called, reversing the order of the strings in myList.
  3. The modified list contents are printed, showing the reversed order {"date", "cherry", "banana", "apple"}.

Output:

List contents before reverse: apple banana cherry date
List contents after reverse: date cherry banana apple

Example 3: Reversing a List of Complex Objects

This example demonstrates how to reverse the order of a list containing complex objects:

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 = {
        {"Alice", 30},
        {"Bob", 25},
        {"Charlie", 35},
        {"Diana", 28}
    };

    std::cout << "List contents before reverse: " << std::endl;
    for (const auto& person : people) {
        std::cout << person.name << " (" << person.age << ")" << std::endl;
    }

    // Reverse the list
    people.reverse();

    std::cout << "List contents after reverse: " << std::endl;
    for (const auto& person : people) {
        std::cout << person.name << " (" << person.age << ")" << std::endl;
    }

    return 0;
}

Explanation:

  1. A std::list named people is initialized with objects of the Person struct, each containing a name and age.
  2. The reverse function is called, reversing the order of the Person objects in the list.
  3. The modified list contents are printed, showing the reversed order of the Person objects.

Output:

List contents before reverse: 
Alice (30)
Bob (25)
Charlie (35)
Diana (28)
List contents after reverse: 
Diana (28)
Charlie (35)
Bob (25)
Alice (30)