
- 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::swap( ) in C++ STL
Given is the task to show the working of forward_list::swap( ) function in C++.
What is a Forward List?
Forward list is a sequence container that allow constant time insert and erase operations anywhere within the sequence. Forward list are implement as a singly-linked lists. The ordering is kept by the association to each element of a link to the next element in the sequence.
What is forward_list::swap()?
forward_list::swap( ) is a function in c++ standard library function which is used to swap the contents of one list to another list of same size and same data type.
Syntax
forward_list1.swap(forward_list2)
Or
swap(forward_list first, forward_list second)
Example
Output – First list : 57 99 54 34 84 Second list : 45 65 78 96 77 After swapping operation outputs are First list : 45 65 78 96 77 Second list : 57 99 54 34 84 Output – First list : 44 37 68 94 73 Second list : 20 11 87 29 40 After swapping operation outputs are First list : 20 11 87 29 40 Second list : 44 37 68 94 73
Approach can be followed
First initialize the two forward list.
Then we print the two forward list.
Then we defines the swap( ) function.
Then we print the forward list after swaping.
By using above approach we can swap the two forward list.
Algorithm
Start −
STEP 1 – Intialize the two forward list and print them First list forward_list<int> List1 = { 10, 20, 30, 40, 50 } for( auto x = list1.start( ); x != list1.end( ); ++x ) cout<< *x << “ “ ; second list forward_list<in> list2 = { 40, 30, 20, 10, 50 } for( auto x = list2.start( ); x != list2.end( ); ++x ) cout<< *x << “ “ ; END STEP 2 – create the swap function to perform swap operation. swap( list1, list2) END Stop
Example
// C++ code to demonstrate the working of forward_list::reverse( ) #include<iostream.h> #include<forward_list.h> Using namespace std; Int main( ){ // initializing two forward lists forward_list<int> list1 = { 10, 20, 30, 40, 50 } cout<< “ Elements of List1:”; for( auto x = list1.start( ); x != list1.end( ); ++x ) cout<< *x << “ “ ; forward_list<int> list2 = { 40, 30, 20, 10, 50 } cout<< “Elements of List2:”; for( auto x = list2.start( ); x != list2.end( ); ++x ) cout<< *x << “ “ ; // defining of function that performs the swap operation swap(list1, list2); cout<< “ After swapping List1 is :”; for(auto x = list1.start( ); x != list1.end( ); ++x) cout<< *x<< “ “; cout<< “ After swapping List2 is :”; for(auto x = list1.start( ); x!= list2.end( ); ++x) cout<< *x<< “ “; return 0; }
Output
If we run the above code then it will generate the following output
OUTPUT – Elements of List1 : 10 20 30 40 50 Elements of list2 : 40 30 20 10 50 After Swapping List1 : 40 30 20 10 50 After Swapping List2 : 10 20 30 40 50 OUTPUT – Elements of List1 : 23 56 78 49 11 Elements of List2 : 11 49 78 56 23 After Swapping List1 : 11 49 78 56 23 After Swapping List1 : 23 56 78 49 11
- 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::reverse( ) 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
