Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6362535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:55:15+00:00 2026-05-24T23:55:15+00:00

Is there any way to do std::set_intersection on two different types of sets? I

  • 0

Is there any way to do std::set_intersection on two different types of sets?

I have two sets:

std::set<X1> l_set1;
std::set<X2> l_set2;

I’m able to define some comparator for them that checks if X1 and X2 are equal.

struct sample_comparer
{
    bool operator()(const &X1 p_left, const &X2 p_right)
    {
        return p_left == p_right;
    }
};

Now, I try to do a set intersection on those two sets:

std::set<X1> l_intersect;
std::set_intersection(l_set1.begin(), l_set1.end(), l_set2.begin(), l_set2.end(),
                      std::inserter(l_intersect, l_intersect.begin()), sample_comparer());

Unfortunately, I can’t force this code to work. I’m not even sure if this is possible, but from the description of set_intersection I know that I can use two different iterators.

I tried to search for some code samples that do what I want, but didn’t found any? Could someone present me a working code sample for my problem?

Update:
the error is:

error: stl_function.h:227: no match for ‘operator<‘ in ‘__x < __y’

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:55:15+00:00Added an answer on May 24, 2026 at 11:55 pm

    The comment by PlasmaHH is likely the problem.

    The way functions like set_intersection work is they first do: a < b and then b < a

    As a result ample_comparer needs to be able to compare both ways:

    struct sample_comparer
    {
        bool operator()(const &X1 p_left, const &X2 p_right)
        {
            return p_left == p_right;
        }
        bool operator()(const &X2 p_left, const &X1 p_right)
        {
            return p_left == p_right;
        }
    };
    

    The following doesn’t actually do anything sensible – but it does compile cleanly:

    struct A
    {
      struct Compare { bool operator () (A const &, A const &) { return false;}  };
    };
    
    struct B
    {
      struct Compare { bool operator () (B const &, B const &) { return false; } };
    };
    
    typedef std::set<A, A::Compare> S1;
    typedef std::set<B, B::Compare> S2;
    
    class IntersectionCompare
    {
    public:
      bool operator ()(S1::value_type, S2::value_type) { return false; } 
      bool operator ()(S2::value_type, S1::value_type) { return false; } 
    };
    
    void bar (S1 & s1, S2 & s2)
    {
      S1 result;
      std::set_intersection (s1.begin ()
          , s1.end ()
          , s2.begin ()
          , s2.end ()
          , std :: insert_iterator< S1 > (result, result.end ())
          , IntersectionCompare ());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way how to set std::setw manipulator (or its function width )
As title says, is there any way to pull something like this up?: std::set<boost::shared_ptr<MyClass>>
Is there any way to use different types of iterators in different vectors? Or,
Is there any way to have something that looks just like a file on
Is there any way to use anonymous classes in C++ as return types? I
std::vector::erase() does not accept reverse iterator. Is there any way to call this method
I have class containing an std::set of boost::weak_ptr<T> . I have two functions begin()
Is there any way to return an STL iterator to a std::map (e.g. std::map<const
I'm adding two different elements to both std::list and std::set and I want the
Is there any way to check whether a file is locked without using a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.