how to find the intersection of two std::set in C++?

后端 未结 5 1134
我寻月下人不归
我寻月下人不归 2020-12-04 10:43

I have been trying to find the intersection between two std::set in C++, but I keep getting an error.

I created a small sample test for this

#include         


        
5条回答
  •  情歌与酒
    2020-12-04 11:13

    You havent provided an output iterator for set_intersection

    template 
    OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
                                    InputIterator2 first2, InputIterator2 last2,
                                    OutputIterator result );
    

    Fix this by doing something like

    ...;
    set intersect;
    set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),
                      std::inserter(intersect,intersect.begin()));
    

    You need a std::insert iterator since the set is as of now empty. We cannot use back_ or front_inserter since set doesnt support those operations.

提交回复
热议问题