在本教程中,我们将讨论一个程序,以了解使用C ++中的STL进行的各种合并操作。
该merge()函数用于合并两个已排序的容器,使新容器也被排序。还includes()用于检查第二个容器中是否存在来自第一个容器的元素。
#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};
//初始化结果向量
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