A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/cpp_standard_library/cpp_forward_list_swap_list.htm below:

C++ Forward_list Library - swap() Function

C++ Forward_list Library - swap() Function Description

The C++ function std::forward_list::swap() exchanges the contents of the first forward_list with another. This function changes size of forward_list if necessary.

Declaration

Following is the declaration for std::forward_list::swap() function form std::forward_list header.

C++11
template <class T, class Alloc>
void swap (forward_list<T,Alloc>& first, forward_list<T,Alloc>& second);
Parameters Return value

None

Exceptions

This function never throws exception.

Time complexity

Linear i.e. O(n)

Example

The following example shows the usage of std::forward_list::swap() function.

#include <iostream>
#include <forward_list>

using namespace std;

int main(void) {

   forward_list<int> fl1 = {1, 2, 3, 4, 5};;
   forward_list<int> fl2 = {10, 20, 30};

   cout << "List fl1 contents before swap operation" << endl;

   for (auto it = fl1.begin(); it != fl1.end(); ++it)
      cout << *it << endl;

   cout << "List fl2 contents before swap operation" << endl;

   for (auto it = fl2.begin(); it != fl2.end(); ++it)
      cout << *it << endl;

   swap(fl1, fl2);

   cout << endl;

   cout << "List fl1 contents after swap operation" << endl;

   for (auto it = fl1.begin(); it != fl1.end(); ++it)
      cout << *it << endl;

   cout << "List fl2 contents after swap operation" << endl;

   for (auto it = fl2.begin(); it != fl2.end(); ++it)
      cout << *it << endl;
   return 0;
}

Let us compile and run the above program, this will produce the following result −

List fl1 contents before swap operation
1
2
3
4
5
List fl2 contents before swap operation
10
20
30

List fl1 contents after swap operation
10
20
30
List fl2 contents after swap operation
1
2
3
4
5

forward_list.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4