Merge two sorted arrays | C ++ program to merge two sorted arrays using maps ; Function to merge arrays ; Declaring a map . using map as a inbuilt tool to store elements in sorted order . ; Inserting values to a map . ; Printing keys of the map . ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void mergeArrays ( int a [ ] , int b [ ] , int n , int m ) { map < int , bool > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ a [ i ] ] = true ; for ( int i = 0 ; i < m ; i ++ ) mp [ b [ i ] ] = true ; for ( auto i : mp ) cout << i . first << " ▁ " ; } int main ( ) { int a [ ] = { 1 , 3 , 5 , 7 } , b [ ] = { 2 , 4 , 6 , 8 } ; int size = sizeof ( a ) / sizeof ( int ) ; int size1 = sizeof ( b ) / sizeof ( int ) ; mergeArrays ( a , b , size , size1 ) ; return 0 ; }