






















/** @file arrInteraction.cpp * @note All Right Reserved. * @brief * @author * @date 2020/4/15 * @note * @history * @warning */ #include <iostream> #include <stdio.h> #include <vector> #include <map> #include <string> #include <algorithm> #include <assert.h> using namespace std; bool vecCmp(const int &num1, const int &num2) { if(num1 < num2) return true; return false; } int main() { std::vector<int> v1{ 1,2,3,4,5,6,7,8 }; std::vector<int> v2{ 5, 7, 9,10 }; std::sort(v1.begin(), v1.end(), vecCmp); std::sort(v2.begin(), v2.end(), vecCmp); cout << "v1: "; for(auto &v1elem: v1) { cout << v1elem << " "; } cout << endl; cout << "v2: "; for(auto &v2elem: v2) { cout << v2elem << " "; } cout << endl; std::vector<int> v_intersection; cout << "v1 and v2 intersection:" << endl; std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v_intersection), vecCmp); for (int n : v_intersection) std::cout << n << ' '; std::vector<int> v_difference; // 在v1但不在v2中 set_difference(v1.begin(), v1.end(), v_intersection.begin(), v_intersection.end(), inserter(v_difference, v_difference.begin()), vecCmp); cout << endl << "in v1 but not in v2:" << endl; for (int n : v_difference) cout << n << " "; cout << endl; printf("hello world!\n"); return 0; }
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。