使用 C++ 中的 STL 进行合并运算 | merge()、includes()、set_union()、set_intersection()、set_difference()、inplace_merge
在本教程中,我们将讨论一个程序,以了解使用 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};
//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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP