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,
- Quickly merging two sorted arrays using std::merge() in C++ STL
- merge() in C++ STL
- list merge() function in C++ STL
- std::sort() in C++ STL
- Kruskal's Minimum Spanning Tree using STL in C++
- Dijkstra’s shortest path algorithm using set in STL
- Dijkstra's Shortest Path Algorithm using priority_queue of STL
- Prim's algorithm using priority_queue in STL
- unordered_map in C++ STL
- fill() and fill_n() functions in C++ STL
- std::transform() in C++ STL (Perform an operation on all elements)
- Permutations of a given string using STL
- Array algorithms in C++ STL (all_of, any_of, none_of, copy_n and iota)
- Iterators in C++ STL
- accumulate() and partial_sum() in C++ STL : numeric header
- std::partition in C++ STL
- Heap in C++ STL | make_heap(), push_heap(), pop_heap(), sort_heap(), is_heap, is_heap_until()
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())
- Counts of distinct consecutive sub-string of length two using C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.