
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
forward_list::reverse( ) in C++ STL
Given is the task to show the working of forward_list::reverse( ) function in C++ STL.
What is a Forward List?
Forward list can be understood as a singly linked list where the tracking can be done only in forward direction but not in a backward direction whereas in the list we can track the elements in both the direction i.e. the element holds the two links one is for forward element and another one is for backward element. Forward lists are therefore fast because they have to hold only one link which will be of a forward element. Forward elements can be inserted and deleted in a constant time.
What is a forward_list::reverse( ) function?
forward_list::reverse( ) is a function in C++ Standard Template Library(STL) that is used to reverse the order of the elements present in the forward list.
Syntax
forwardlist_name.reverse( )
Parameter
This function doesn’t have any parameter.
Return Value
This function doesn’t have any return value. It only performs operation of reversing the list
Example
Input-: List of elements are: 57 99 54 34 84 Output–: Reversed elements of list are: 84 34 54 99 57 Input-: List of elements are: 40 30 60 90 70 Output–: Reversed elements of list are: 70 90 60 30 40
Approach that is used in the below program is as follows
First initialize the list.
Then we will print the forward list before applying reverse () function.
Then we defines the forward.reverse( ) function present in a header file in C++.
Then we will display the reversed forward list
Example
// C++ code to demonstrate the working of forward_list::reverse( ) #include<iostream.h> #include<forward_list.h> Using namespace std; Int main( ){ // initializing forward list forward_list<int> forward = {10,20,30,40,50}; cout<< “ List of elements : ”; for(auto it = forward.start( ); it != forward.end( ); ++it) cout<< *it<< “ “; // defining of function that performs the reverse operation forward.reverse( ); cout<< “ Reversed elements list”; for( auto it =forward.start( ); it != forward.end( ); ++it) cout<< *it<< “ “; return 0; }
Output
If we run the above code then it will generate the following output
Reversed elements list : 50 40 30 20 10
- Related Articles
- forward_list::begin() and forward_list::end() in C++ STL
- forward_list::front() and forward_list::empty() in C++ STL
- forward_list::push_front() and forward_list::pop_front() in C++ STL
- forward_list::clear() and forward_list::erase_after() in C++ STL
- forward_list cbegin() in C++ STL
- forward_list::cbefore_begin() in C++ STL
- forward_list::unique( ) in C++ STL
- forward_list::swap( ) in C++ STL
- forward_list merge() in C++ STL
- forward_list::before_begin() in C++ STL
- Forward_list::operator = in C++ STL
- forward_list::remove() in C++ STL
- C++ Program to Implement Forward_List in STL
- forward_list emplace_after() and emplace_front() in C++ STL
- forward_list max_size() in C++ STL with Examples
