이 튜토리얼에서는 C++에서 STL을 사용하여 다양한 병합 작업을 이해하는 프로그램에 대해 설명합니다.
merge() 함수는 새 컨테이너도 정렬되는 방식으로 두 개의 정렬된 컨테이너를 병합하는 데 사용됩니다. 추가 Include()는 첫 번째 컨테이너의 요소가 두 번째 컨테이너에 있는지 확인하는 데 사용됩니다.
예시
#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;
} 출력
The new container after merging is : 1 1 3 4 5 5 6 7 20 25 30 30 v4 includes v1