C++ 程式以在 STL 中實現 Set_Intersection
兩個集合的交集僅由兩個集合中公用的元素組成。函式複製的元素始終按相同順序來自第一個集合。兩個集合中的元素已經排列。
常見的集合運算有 −
- 集合並集
- 集合交集
- 對稱集合差集或異或
- 集合差集或減法

演算法
Begin Declare set vector v and iterator st. Initialize st = set_intersection (set1, set1 + n, set2, set2 +n, v.begin())) Print the intersection between two sets. End.
示例程式碼
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main () {
int set1[] = {5,6,7,8,9,10};
int set2[] = {1,2,3,4,6,7};
vector<int> v(10);
vector<int>::iterator st;
sort (set1, set1 + 6);
sort (set2, set2 + 6);
st = set_intersection (set1, set1 + 6, set2, set2 + 6, v.begin());
v.resize(st - v.begin());
cout << "The intersection between the two set has " << (v.size()) << " elements: "<<endl;
for (st = v.begin(); st != v.end(); ++st)
cout<< *st<<" ";
cout <<endl;
return 0;
}輸出
The intersection between the two set has 2 elements: 6 7
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP