C++算法 set_union()函数用于查找两个排序参数[first1,last1)和[first2,last2)的并集,这两个元素由任一集合中的元素形成或两者兼而有之。
default (1) template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result); custom (2) template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator set_union (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);
first1 :一个输入迭代器,它指向两个已排序源序列中第一个的第一个元素。
last1 :一个输入迭代器,它指向两个已排序源序列中第一个序列中的最后一个元素。
first2 :一个输入迭代器,它指向第二个排序的源序列中的第一个元素。
last2 :一个输入迭代器,指向第二个排序的源序列中的最后一个元素。
comp :用户定义的二进制谓词函数,该函数接受两个参数,如果两个参数顺序正确,则返回true,否则返回false。
result:一个输出迭代器,它指向目标参数中第一个元素的位置。
此函数将迭代器返回到构造参数的末尾。
让我们看一个简单的示例来演示set_union()的用法:
#include <iostream> #include <set> #include <list> #include <vector> #include <algorithm> #include <iterator> using namespace std; int main() { list<int> a = {1, 2, 3, 4}; multiset<int> b = {4, 5, 6, 2}; vector<int> result; set_union(begin(a), end(a), begin(b), end(b), inserter(result, end(result))); for_each(begin(result), end(result), [](int x) { cout << x << endl; }); return 0; }
输出:
1 2 3 4 5 6
让我们看另一个简单的例子:
#include <iostream> //std::cout #include <algorithm> //std::set_union, std::sort #include <vector> //std::vector using namespace std; int main() { int first[] = { 5, 10, 15, 20, 25 }; int second[] = { 50, 40, 30, 20, 10 }; int n = sizeof(first)/sizeof(first[0]); //Print first array cout << "First array contains:"; for (int i = 0; i < n; i++) cout << " " << first[i]; cout << "\n"; //Print second array cout << "Second array contains:"; for (int i = 0; i < n; i++) cout << " " << second[i]; cout << "\n\n"; vector<int> v(10); vector<int>::iterator it, st; sort(first, first + n); sort(second, second + n); //Using default function it = set_union(first, first + n, second, second + n, v.begin()); cout << "The union has " << (it - v.begin()) << " elements:\n"; for (st = v.begin(); st != it; ++st) cout << ' ' << *st; cout << '\n'; return 0; }
输出:
First array contains: 5 10 15 20 25 Second array contains: 50 40 30 20 10 The union has 8 elements: 5 10 15 20 25 30 40 50
让我们看另一个简单的示例,查找同时参加这两个科目的所有学生的列表:
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; // Driver code int main() { string first[] = { "Nikita", "Divya", "Deep", "Kashish" }; string second[] = { "Aman", "Nikita", "Amita", "Deep" }; int n = sizeof(first)/sizeof(first[0]); //Print students of first list cout << "Students in first subject:"; for (int i = 0; i < n; i++) cout << " " << first[i]; cout << "\n"; //Print students of second list cout << "Students in second subject:"; for (int i = 0; i < n; i++) cout << " " << second[i]; cout << "\n\n"; vector<string> v(10); vector<string>::iterator it, st; //Sorting both the list sort(first, first + n); sort(second, second + n); //Using default operator< it = set_union(first, first + n, second, second + n, v.begin()); cout << "Students attending both subjects are:\n"; for (st = v.begin(); st != it; ++st) cout << ' ' << *st; cout << '\n'; return 0; }
输出:
Students in first subject: Nikita Divya Deep Kashish Students in second subject: Aman Nikita Amita Deep Students attending both subjects are: Aman Amita Deep Divya Kashish Nikita
让我们看一个简单的例子:
#include <vector> #include <iostream> #include <algorithm> #include <iterator> using namespace std; int main() { vector<char> v1 = {'A', 'B', 'C'}; vector<char> v2 = { 'C', 'D', 'E', 'F'}; vector<char> dest1; set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(dest1)); for (const auto &i : dest1) { cout << i << ' '; } cout << '\n'; return 0; }
输出:
A B C D E F
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Tony Bai · Go语言第一课 -〔Tony Bai〕