Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


C++ Programming Code Examples

Learn C++ Language

Algorithm Library merge() Function in C++ Programming Language

Algorithm Library merge() Function in C++
Merge sorted ranges. Combines the elements in the sorted ranges [first1,last1) and [first2,last2), into a new range beginning at result with all its elements sorted. The C++ algorithm::merge function is used to merge elements of sorted ranges [first1, last1) and [first2, last2) to the range starting at result. In default version elements are compared using operator< and in custom version elements are compared using comp. The elements are compared using operator< for the first version, and comp for the second. The elements in both ranges shall already be ordered according to this same criterion (operator< or comp). The resulting range is also sorted according to this.
Syntax for Algorithm merge() Function in C++
#include <algorithm> //default (1) template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator merge (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); //custom (2) template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator merge (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
first1, last1
Input iterators to the initial and final positions of the first sorted sequence. The range used is [first1,last1), which contains all the elements between first1 and last1, including the element pointed by first1 but not the element pointed by last1.
first2, last2
Input iterators to the initial and final positions of the second sorted sequence. The range used is [first2,last2).
result
Output iterator to the initial position of the range where the resulting combined range is stored. Its size is equal to the sum of both ranges above.
comp
Binary function that accepts two arguments of the types pointed by the iterators, and returns a value convertible to bool. The value returned indicates whether the first argument is considered to go before the second in the specific strict weak ordering it defines. The function shall not modify any of its arguments. This can either be a function pointer or a function object. The ranges shall not overlap. The elements in both input ranges should be assignable to the elements in the range pointed by result. They should also be comparable (with operator< for (1), and with comp for (2)). Function returns an iterator pointing to the past-the-end element in the resulting sequence.
Complexity
Up to linear in (1+count1-count2), where countX is the distance between firstX and lastX: Compares and assigns all elements.
Data races
The objects in the ranges [first1,last1) and [first2,last2)are accessed. The objects in the range between result and the returned value are modified.
Exceptions
Throws if any of element comparisons, the element assignments or the operations on iterators throws. Note that invalid arguments cause undefined behavior.
/* C++ Algorithm merge() function is used to merge two sorted ranges [first1, last1) and [first2, last2) into one sorted range beginning at result. */ /* Merge sorted ranges merge() function code example. */ #include <bits/stdc++.h> using namespace std; int main() { // initializing 1st container vector<int> arr1 = { 1, 4, 6, 3, 2 }; // initializing 2nd container vector<int> arr2 = { 6, 2, 5, 7, 1 }; // declaring resultant container vector<int> arr3(10); // sorting initial containers sort(arr1.begin(), arr1.end()); sort(arr2.begin(), arr2.end()); // using merge() to merge the initial containers merge(arr1.begin(), arr1.end(), arr2.begin(), arr2.end(), arr3.begin()); // printing the resultant merged container cout << "The container after merging initial containers is : "; for (int i = 0; i < arr3.size(); i++) cout << arr3[i] << " "; return 0; }


Takes the input of the number of 'vertexes' & the number of edges in the graph. It takes the input of vertex pairs for the given number of edges. It generates a line graph for the given










A Queue Node (Queue is implemented using Doubly Linked List). And a FIFO collection of Queue Nodes. A hash (Collection of pointers to Queue Nodes). A utility function to create