一些合并操作类在头文件“算法”下在C++ STL中提供,这有助于轻松地进行多个合并操作。 下面提到了其中一些。
null
- 合并(beg1,end1,beg2,end2,beg3) :-此函数用于合并两个已排序的容器,并按排序顺序(合并排序)存储在新容器中。它有5个参数,第一个容器的第一个和最后一个迭代器,第二个容器的第一个和最后一个迭代器,以及结果容器的第一个迭代器。
- 包括(beg1,end1,beg2,end2) :-此函数用于检查一个已排序的容器元素是否包含其他已排序的容器元素。如果第一个容器包含第二个容器,则返回true,否则返回false。
CPP
// C++ code to demonstrate the working of // merge() and include() #include<iostream> #include<algorithm> // merge operations #include<vector> // for vector using namespace std; int main() { // Initializing 1st vector vector< int > v1 = {1, 3, 4, 5, 20, 30}; // Initializing 2nd vector vector< int > v2 = {1, 5, 6, 7, 25, 30}; // Declaring resultant vector // for merging vector< int > v3(12); // Using merge() to merge vectors v1 and v2 // and storing result in v3 merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()); // Displaying resultant container cout << "The new container after merging is :"; for ( int &x : v3) cout << x << " "; cout << endl; // Initializing new vector vector< int > v4 = {1, 3, 4, 5, 6, 20, 25, 30}; // Using include() to check if v4 contains v1 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
- 就地合并(beg1、beg2、end):- 此函数用于在单个容器中对两个连续放置的排序范围进行排序。它需要3个参数,迭代器到第一个排序范围的开头,迭代器到第二个排序范围的开头,迭代器到最后一个位置。
CPP
// C++ code to demonstrate the working of // inplace_merge() #include<iostream> #include<algorithm> // merge operations #include<vector> // for vector using namespace std; int main() { // Initializing 1st vector vector< int > v1 = {1, 3, 4, 5, 20, 30}; // Initializing 2nd vector vector< int > v2 = {1, 5, 6, 7, 25, 30}; // Declaring resultant vector // for inplace_merge() vector< int > v3(12); // using copy to copy both vectors into // one container auto it = copy(v1.begin(), v1.end(), v3.begin()); copy(v2.begin(), v2.end(), it); // Using inplace_merge() to sort the container inplace_merge(v3.begin(),it,v3.end()); // Displaying resultant container cout << "The new container after inplace_merging is :"; for ( int &x : v3) cout << x << " "; cout << endl; return 0; } |
- 输出:
The new container after inplace_merging is :1 1 3 4 5 5 6 7 20 25 30 30
- 集合_并集(beg1,end1,beg2,end2,beg3) :-此函数计算两个容器的集合并集,并存储在新容器中。它将迭代器返回到结果容器的最后一个元素。它有5个参数,第一个容器的第一个和最后一个迭代器,第二个容器的第一个和最后一个迭代器,以及结果容器的第一个迭代器。应对容器进行分类,有必要将新容器的大小调整到合适的大小。
- 集合交叉点(beg1,end1,beg2,end2,beg3) :-此函数计算两个容器的集合交集,并存储在新容器中。它将迭代器返回到结果容器的最后一个元素。它有5个参数,第一个容器的第一个和最后一个迭代器,第二个容器的第一个和最后一个迭代器,以及结果容器的第一个迭代器。应对容器进行分类,有必要将新容器的大小调整到合适的大小。 可以找到一种在排序范围内实现集合并集和集合交集的方法 在这里
CPP
// C++ code to demonstrate the working of // set_union() and set_intersection() #include<iostream> #include<algorithm> // for merge operations #include<vector> // for vector using namespace std; int main() { // Initializing 1st vector vector< int > v1 = {1, 3, 4, 5, 20, 30}; // Initializing 2nd vector vector< int > v2 = {1, 5, 6, 7, 25, 30}; // Declaring resultant vector // for union vector< int > v3(10); // Declaring resultant vector // for intersection vector< int > v4(10); // using set_union() to compute union of 2 // containers v1 and v2 and store result in v3 auto it = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()); // using set_intersection() to compute intersection // of 2 containers v1 and v2 and store result in v4 auto it1 = set_intersection(v1.begin(),v1.end(), v2.begin(), v2.end(), v4.begin()); // resizing new container v3.resize(it - v3.begin()); // resizing new container v4.resize(it1 - v4.begin()); // Displaying set union cout << "Union of two containers is : "; for ( int &x : v3) cout << x << " "; cout << endl; // Displaying set intersection cout << "Intersection of two containers is : "; for ( int &x : v4) cout << x << " "; cout << endl; return 0; } |
- 输出:
Union of two containers is : 1 3 4 5 6 7 20 25 30 Intersection of two containers is : 1 5 30
- 设置差异(beg1,end1,beg2,end2,beg3) :-此函数计算两个容器的设置差异,并存储在新容器中。它将迭代器返回到结果容器的最后一个元素。它有5个参数,第一个容器的第一个和最后一个迭代器,第二个容器的第一个和最后一个迭代器,以及结果容器的第一个迭代器。应对容器进行分类,有必要将新容器的大小调整到合适的大小。
- s 对称差(beg1,end1,beg2,end2,beg3) :-此函数计算两个容器的设置对称差,并存储在新容器中。它将迭代器返回到结果容器的最后一个元素。它有5个参数,第一个容器的第一个和最后一个迭代器,第二个容器的第一个和最后一个迭代器,以及结果容器的第一个迭代器。应对容器进行分类,有必要将新容器的大小调整到合适的大小。
CPP
// C++ code to demonstrate the working of // set_difference() and set_symmetric_difference() #include<iostream> #include<algorithm> // for merge operations #include<vector> // for vector using namespace std; int main() { // Initializing 1st vector vector< int > v1 = {1, 3, 4, 5, 20, 30}; // Initializing 2nd vector vector< int > v2 = {1, 5, 6, 7, 25, 30}; // Declaring resultant vector // for difference vector< int > v3(10); // Declaring resultant vector // for symmetric_difference vector< int > v4(10); // using set_difference() to compute difference // of 2 containers v1 and v2. auto it = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()); // using set_symmetric_difference() to compute // symmetric_difference/ of 2 containers auto it1 = set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v4.begin()); // resizing new container v3.resize(it - v3.begin()); // resizing new container v4.resize(it1 - v4.begin()); // Displaying set difference cout << "Difference of two containers is : "; for ( int &x : v3) cout << x << " "; cout << endl; // Displaying set symmetric_difference cout << "symmetric_difference of two containers is : "; for ( int &x : v4) cout << x << " "; cout << endl; return 0; } |
- 输出:
Difference of two containers is : 3 4 20 Symmetric difference of two containers is : 3 4 6 7 20 25
本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END