c++ vector点集取交集
时间: 2023-11-29 18:47:42 浏览: 96
以下是两种C++ vector点集取交集的例子:
引用:使用multiset实现
```cpp
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int main() {
int iarr1[] = {1, 2, 3, 3, 4, 5, 6, 7, 9};
int iarr2[] = {1, 4, 3, 9, 10};
multiset<int> iset1(begin(iarr1), end(iarr1));
multiset<int> iset2(begin(iarr2), end(iarr2));
vector<int> ivec(20);
auto iter = set_intersection(iset1.begin(), iset1.end(), iset2.begin(), iset2.end(), ivec.begin());
ivec.resize(iter - ivec.begin());
for (auto i : ivec) {
cout << i << " ";
}
return 0;
}
```
引用:使用sort实现
```cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int iarr1[] = {1, 2, 3, 3, 6, 7, 4, 5};
int iarr2[] = {1, 4, 3, 10, 9};
sort(begin(iarr1), end(iarr1));
sort(begin(iarr2), end(iarr2));
vector<int> ivec(10);
auto iter = set_intersection(begin(iarr1), end(iarr1), begin(iarr2), end(iarr2), ivec.begin());
ivec.resize(iter - ivec.begin());
for (auto i : ivec) {
cout << i << " ";
}
return 0;
}
```
阅读全文