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 8851715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:13:22+00:00 2026-06-14T13:13:22+00:00

I have been trying to find the intersection between two std::set in C++, but

  • 0

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 <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;

int main() {
  set<int> s1;
  set<int> s2;

  s1.insert(1);
  s1.insert(2);
  s1.insert(3);
  s1.insert(4);

  s2.insert(1);
  s2.insert(6);
  s2.insert(3);
  s2.insert(0);

  set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end());
  return 0;
}

The latter program does not generate any output, but I expect to have a new set (let’s call it s3) with the following values:

s3 = [ 1 , 3 ]

Instead, I’m getting the error:

test.cpp: In function ‘int main()’:
test.cpp:19: error: no matching function for call to ‘set_intersection(std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>, std::_Rb_tree_const_iterator<int>)’

What I understand out of this error, is that there’s no definition in set_intersection that accepts Rb_tree_const_iterator<int> as a parameter.

Furthermore, I suppose the std::set.begin() method returns an object of such type,

Is there a better way to find the intersection of two std::set in C++? Preferably a built-in function?

  • 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-06-14T13:13:24+00:00Added an answer on June 14, 2026 at 1:13 pm

    You haven’t provided an output iterator for set_intersection

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

    Fix this by doing something like

    ...;
    set<int> 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 std::back_inserter or std::front_inserter since set doesn’t support those operations.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to find a solution to this one but could not
I have been trying to find out what the difference is between the IS
I have been trying out to figure out a way to find intersection of
I have been trying to find info on the web about the differences between
I have been trying to find out more about Voldemort but it doesn't seem
I have been trying to find this issue here, but no topic seems to
I have been trying to find an answer to my question but couldnt. Now,
I have been trying to find the answer to this simple question - but
I have been trying to find a solution for this for a while but
Have been trying to find the error in one line of code. Return expected

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.