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
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程