
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), inplace_merge
In this tutorial, we will be discussing a program to understand the various merge operations using STL in C++.
The merge() function is used to merge two sorted containers in a way that the new container is also sorted. Further includes() is used to check if the elements from first container are present in the second one.
Example
#include<iostream> #include<algorithm> #include<vector> using namespace std; int main(){ vector<int> v1 = {1, 3, 4, 5, 20, 30}; vector<int> v2 = {1, 5, 6, 7, 25, 30}; //initializing resultant vector vector<int> v3(12); merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()); cout << "The new container after merging is :\n"; for (int &x : v3) cout << x << " "; cout << endl; vector<int> v4 = {1, 3, 4, 5, 6, 20, 25, 30}; includes(v4.begin(), v4.end(), v1.begin(), v1.end())? cout << "v4 includes v1": cout << "v4 does'nt include v1"; return 0; }
Output
The new container after merging is : 1 1 3 4 5 5 6 7 20 25 30 30 v4 includes v1
Advertisements