当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ Algorithm set_intersection()用法及代码示例


C++算法set_intersection()函数用于求两个已排序范围[first1,last1)和[first2,last2)的交集,该交集仅由两个集合中都存在的元素构成。

对于第一个版本使用运算符 < 比较元素,或者对于第二个版本使用给定的二进制比较函数 comp 比较元素。

用法

default (1)   template <class InputIterator1, class InputIterator2, class OutputIterator>
                   OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
                        InputIterator2 first2, InputIterator2 last2, OutputIterator result);

custom (2)   template <class InputIterator1, class InputIterator2,
                   class OutputIterator, class Compare>
                    OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1,
                InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp);

参数

first1:指向两个已排序源序列中第一个中的第一个元素的输入迭代器。

last1:一个输入迭代器,指向两个已排序源序列中第一个的最后一个元素。

first2:指向第二个排序源序列中第一个元素的输入迭代器。

last2:指向第二个已排序源序列中过去最后一个元素的输入迭代器。

comp: 一个用户定义的二元谓词函数,它接受两个参数,如果两个参数按顺序返回真,否则返回假。它遵循严格的弱排序来对元素进行排序。

result:一个输出迭代器寻址到目标范围中第一个元素的位置。

返回值

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

复杂度

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

数据竞争

范围 [first1, last1) 和 [first2. last2) 被访问。

结果和返回值之间范围内的对象被修改。

异常

如果任何元素比较、元素赋值或迭代器上的操作引发异常,则此函数将引发异常。

注意:无效的参数会导致未定义的行为。

例子1

让我们看一个简单的例子来演示 set_intersection() 的使用:

#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_intersection(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;
}

输出:

2
4

例子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_intersection(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 2 elements:
 10 20

例子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_intersection(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:
 Deep Nikita

示例 4

让我们看一个简单的例子:

#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;
 
int main()
{ 
    vector<char> v1 = {'A', 'B', 'C'}; 
    vector<char> v2 = {      'B', 'C', 'D', 'E', 'F'}; 
    
    vector<char> dest1;
 
    set_intersection(v1.begin(), v1.end(),
                   v2.begin(), v2.end(),                  
                   back_inserter(dest1));
 
    for (const auto &i:dest1) {
        cout << i << ' ';
    }   
    cout << '\n';
    
    return 0;
}

输出:

B C





相关用法


注:本文由纯净天空筛选整理自 C++ Algorithm set_intersection()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。