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
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
#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:
- A
std::listnamedmyListis initialized with{10, 20, 30, 40}. - The
reversefunction is called, which reverses the order of the elements inmyList. - 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
#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:
- A
std::listof strings namedmyListis initialized with{"apple", "banana", "cherry", "date"}. - The
reversefunction is called, reversing the order of the strings inmyList. - 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
#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:
- A
std::listnamedpeopleis initialized with objects of thePersonstruct, each containing a name and age. - The
reversefunction is called, reversing the order of thePersonobjects in the list. - The modified list contents are printed, showing the reversed order of the
Personobjects.
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)
