C++ Standard Library C++ STL Library

C++ <forward_list> - merge() Function



The C++ forward_list::merge function is used to merge two sorted forward_lists into one. The forward_lists should be sorted into ascending order before calling this function.

The function merges the forward_list other into the given forward_list by transferring all of its elements at their respective ordered positions into the forward_list. The container other becomes empty after the operation, and the whole operation is performed without constructing or destroying any element.

The elements of two forward_lists are compared using operator< (in first version) or comp (in second version).

Syntax

//version 1
void merge (forward_list& other);
void merge (forward_list&& other);

//version 2
template <class Compare>
  void merge (forward_list& other, Compare comp);
template <class Compare>
  void merge (forward_list&& other, Compare comp);

Parameters

other Specify another forward_list object of same type to merge.
comp Specify a binary function that accepts two element as arguments, and returns a value convertible to bool. The returned value indicates whether the first argument is considered to go before the second using the strict weak ordering it defines.

Return Value

None.

Time Complexity

Linear i.e, Θ(n).

Example:

In the example below, the forward_list::merge function is used to merge two sorted forward_lists.

#include <iostream>
#include <forward_list>
using namespace std;
 
int main (){
  forward_list<int> flist1{10, 20, 50, 60};
  forward_list<int> flist2{10, 30, 45, 75};
  forward_list<int>::iterator it;

  cout<<"flist1 contains: ";
  for(it = flist1.begin(); it != flist1.end(); it++)
    cout<<*it<<" ";

  cout<<"\nflist2 contains: ";
  for(it = flist2.begin(); it != flist2.end(); it++)
    cout<<*it<<" ";

  //merging flist2 into flist1 
  flist1.merge(flist2);

  //displaying the result
  cout<<"\n\nAfter merging, flist1 contains: \n";
  for(it = flist1.begin(); it != flist1.end(); it++)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

flist1 contains: 10 20 50 60 
flist2 contains: 10 30 45 75 

After merging, flist1 contains: 
10 10 20 30 45 50 60 75 

Example:

The example below describes the usage of second version of forward_list::merge function, where int value of elements are used for sorting the elements.

#include <iostream>
#include <forward_list>
using namespace std;
 
//a binary predicate to compare int value of elements
bool int_comp (double x, double y)
{ return ( int(x) < int(y) ); }

int main (){
  forward_list<double> flist1{3.5, 3.2, 5.5};
  forward_list<double> flist2{3.3, 5.9, 5.4};
  forward_list<double>::iterator it;

  cout<<"flist1 contains: ";
  for(it = flist1.begin(); it != flist1.end(); it++)
    cout<<*it<<" ";

  cout<<"\nflist2 contains: ";
  for(it = flist2.begin(); it != flist2.end(); it++)
    cout<<*it<<" ";

  //merging flist2 into flist1 
  flist1.merge(flist2, int_comp);

  //displaying the result
  cout<<"\n\nAfter merging, flist1 contains: \n";
  for(it = flist1.begin(); it != flist1.end(); it++)
    cout<<*it<<" ";

  return 0;
}

The output of the above code will be:

flist1 contains: 3.5 3.2 5.5 
flist2 contains: 3.3 5.9 5.4 

After merging, flist1 contains: 
3.5 3.2 3.3 5.5 5.9 5.4 

❮ C++ <forward_list> Library

5