C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ set_union()

C++算法set_union()

C++算法 set_union()函数用于查找两个排序范围[first1,last1)和[
使用第一个版本的运算符 <或使用给定的二进制比较函数comp来比较元素。第二个版本。< div>

语法

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: 一个输出迭代器,寻址到目标范围中第一个元素的位置。

返回值

此函数将迭代器返回到构造范围的末尾。

复杂度

在[first1,last1)和[first2,last2)之间的距离,线性是线性的: 最多执行2 *(count1 + count2)-1个比较。其中count1 = last1-first1,count2 = last2-first2、

数据竞争

范围为[first1,last1)和[first2、 last2)被访问。
修改了结果和返回值之间范围内的对象。

异常

如果任何元素比较,此函数都会引发异常,元素分配或对迭代器的操作将引发异常。
注意: 无效的参数会导致未定义的行为。

示例1

让我们看一个简单的示例来演示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

示例2

让我们看另一个简单的示例:
#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

示例3

让我们看另一个简单的示例,查找同时参加这两个科目的所有学生的名单:
#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

示例4

让我们看一个简单的示例:
#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

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4