forward_list::merge() is an inbuilt function in C++ STL which merges two sorted forward_lists into one.
The merge() function can be used in two ways:
- Merge two forward lists that are sorted in ascending order into one.
- Merge two forward lists into one using a comparison function.
forwardlist_name1.merge(forward_list& forwardlist_name2) or forwardlist_name1.merge(forward_list& forwardlist_name2, Compare comp)
Parameters: The function accepts two parameters which are specified as below:
- forwardlist_name2 – Another forward list of the same type which is to be merged
- comp – A comparison function which should return true or false.
Return value: The function does not return anything.
Below programs illustrate the above function:
List contains following elements 10 12 20 25 30 31 41
List contains following elements 41 31 30 25 20 12 10
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge,
- merge() in C++ STL
- list merge() function in C++ STL
- Quickly merging two sorted arrays using std::merge() in C++ STL
- How to iterate over the elements of an std::tuple in C++
- How to access private/protected method outside a class in C++
- lvalues references and rvalues references in C++ with Examples
- Difference between cout and std::cout in C++
- C++ program to check whether a String is a Pangram or not
- Difference between pair in Multiset and Multimap in C++ STL
- How to use unordered_map efficiently in C++
- Header files in C/C++ with Examples
- C++ Program for the Fractional Knapsack Problem
- Trigraphs in C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.