std::set_intersection 问题
std::set_intersection 求交的时候,如果传入的是vector 必须要同序
源码
template <class InputIterator1, class InputIterator2, class OutputIterator> OutputIterator set_intersection (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result) { while (first1!=last1 && first2!=last2) { if (*first1<*first2) ++first1; else if (*first2<*first1) ++first2; else { *result = *first1; ++result; ++first1; ++first2; } } return result; }
https://www.programiz.com/cpp-programming/online-compiler/
#include <iostream> using namespace std; int main() { cout << "Hello, world!" << endl; std::vector<int> exclude_express_province_code_list {31,21}; std::vector<int> user_city_ids{31,21}; std::set_intersection(exclude_express_province_code_list.begin(), exclude_express_province_code_list.end(), user_city_ids.begin(), user_city_ids.end(), std::back_inserter(exclude_ids)); cout << exclude_ids.size(); return 0; }
输出2
// Online C++ compiler to run C++ program online #include <iostream> #include <vector> #include <algorithm> #include <set> #include <functional> // std::plus int main() { // Write C++ code here // std::cout << "Try programiz.pro"; std::vector<int> exclude_express_province_code_list {22,31}; std::vector<int> user_city_ids{31,22}; std::vector<int> exclude_ids; std::set_intersection(exclude_express_province_code_list.begin(), exclude_express_province_code_list.end(), user_city_ids.begin(), user_city_ids.end(), std::back_inserter(exclude_ids)); std::cout << exclude_ids.size() << std::endl; return 0; }
输出1