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

posted @   xiaoxinlong  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
AmazingCounters.com
点击右上角即可分享
微信分享提示