C++ std::merge with std::vector














































C++ std::merge with std::vector



This article deals about merge() function and we implement it by using vector container.merge() is an algorithm function in algorithm header file.
      merge() algorithm function is used to merge the data(or)elements of two container's.
Definition:
template <class InputIterator1, class InputIterator2, class OutputIterator>
         OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
         InputIterator2 first2, InputIterator2 last2,
                        OutputIterator result);
	
          template <class InputIterator1, class InputIterator2,class OutputIterator, class Compare>
         OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
                        InputIterator2 first2, InputIterator2 last2,
                        OutputIterator result, Compare comp);

NOTE:
       In this program we are merging the elements of two vector containers and storing the result in 
another vector container and printing merged elements.
       sort() function is used to sort the elements of both vector container's.
      
       Executable on C++98/11.
   __________________________________________________________________________
PROGRAM:

#include<algorithm>
#include<iostream> 
#include<vector>
using namespace std; 
  
int main() 
{
    int n,i,array[10],array1[10];
    cout<<"Enter the size:"<<endl;
    cin>>n;
	cout<<"Enter the elements of container 1:"<<endl;
	for(i=0;i<n;i++)
	{
	cin>>array[i];
	}
	cout<<"Enter the elements of container 2:"<<endl;
	for(i=0;i<n;i++)
	{
	cin>>array1[i];
	}
    vector<int>v(array,array+n); // 1st vector container
    vector<int>v1(array1,array1+n);//2nd vector container
    sort(v.begin(),v.end());
    sort(v1.begin(),v1.end());
    vector<int>v2(2*n);//3rd vector container(result)
    vector<int> :: iterator it;
    merge(v.begin(),v.end(),v1.begin(),v1.end(),v2.begin());
    for(it=v2.begin();it!=v2.end();++it)
    {
    	cout<< *it <<" ";
	}
}

OUTPUT:

Enter the size:
6
Enter the elements of container 1:
2
3
1
6
5
7
Enter the elements of container 2:
13
12
17
11
19
21
1 2 3 5 6 7 11 12 13 17 19 21
          

More Articles of Valisetti Abhinav:

Name Views Likes
c++ program to count binary search tree nodes that lie in a given range 1039 15
C++ std::stable_sort with std::vector 759 22
C++ std::stable_sort with std::deque 2089 20
C++ program to construct all possible binary search tress from keys 1 to N 598 17
C++ std::copy with std::forward_list 1114 21
C++ std::binary_search with std::forward_list 671 17
C++ std::merge with std::deque 2990 20
C++ std::merge with std::forward_list 635 13
C++ std::binary_search with std::set 1101 18
C++ std::copy with std::deque 3046 20
C++ program to find next right node of a given key 659 13
C++ std::search_n with std::vector 836 25
C++ std::copy with std::multiset 1325 13
C++ std::search_n with std::array 720 21
C++ program to find maximum in binary tree 1512 26
C++ program to find minimum in binary tree 731 19
C++ std::binary_search with std::deque 1180 19
C++ std::merge with std::multiset 1541 23
C++ program to insert an element into binary search tree 1943 24
C++ std::binary_search with std::list 1056 25
C++ std::search_n with std::forward_list 662 17
C++ std::search_n with std::multiset 665 20
C++ program to print the nodes at odd levels of a tree 705 17
C++ std::binary_search with std::array 1179 27
C++ program to convert binary tree to binary search tree using std::stl 675 15
C++ std::search_n with std::deque 622 20
C++ std::merge with std::vector 705 29
C++ std::copy with std::vector 587 17
C++ std::binary_search with std::vector 722 19
C++ program to find median of binary search tree in O(n). 690 16
C++ std::merge with std::array 694 13

Comments