
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Quickly merging two sorted arrays using std::merge() in C++ STL(cute ho ap)
In this article we will be discussing how we can quickly merge two sorted arrays using std::merge() function in C++ STL.
So, before solving the problem let's first discuss the std::merge() in C++ STL.
What is std::merge()?
std::merge() function is an inbuilt function in C++ STL, which is defined in the <algorithm> header file. merge() is used to merge two sorted ranges or series. This function combines two sorted ranges to make one single sorted range. All the elements are compared using less than operator (<) so the third range is also sorted by on its own.
How we will quickly merge two sorted arrays using std::merge()?
We are given the two sorted arrays which are arr1[] and arr2[] and the task is to merge those two sorted arrays and store it in another empty array which can be arr3[], using the function merge() available in C++ STL.
First we will calculate the size of both arrays arr[size_arr] and brr[size_brr] and define a third array whose size is the sum of sizes of both the arrays final[size_arr+size_brr].
Then we will merge the two arrays into the third array using merge() function like merge(arr, (arr + size_arr), brr, (brr + size_brr), final);
Example
#include <iostream> #include <algorithm> using namespace std; int main(){ int arr[] = {1, 2, 3}; int size_arr = sizeof(arr)/sizeof(arr[0]); int brr[] = {4, 5, 6}; int size_brr = sizeof(brr)/sizeof(brr[0]); int final[size_arr + size_brr]; merge(arr, (arr + size_arr), brr, (brr + size_brr), final); cout<<"Final array after merging the elements is: "; for (int i = 0; i < (size_brr + size_arr); i++) cout << final[i] << " "; return 0; }
Output
Final array after merging the elements is: 1 2 3 4 5 6
- Related Articles
- Merge two sorted arrays using C++.
- Merge two sorted arrays in C#
- Merging two sorted arrays into one sorted array using JavaScript
- Merging two unsorted arrays in sorted order in C++.
- Merge two sorted arrays into a list using C#
- Merge two sorted arrays in Python using heapq?
- Merge two sorted arrays in Java
- Merging sorted arrays together JavaScript
- C# program to merge two sorted arrays into one
- Merge two sorted linked lists using C++.
- Merge two sorted arrays to form a resultant sorted array in JavaScript
- Merge two arrays using C# AddRange() method
- Merge k sorted arrays in Java
- Merge k sorted arrays of different sizes in C++
- Alternatively merging two arrays - JavaScript
