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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:04:57+00:00 2026-05-27T18:04:57+00:00

I was using set_intersection from the STL in C++, and I was wondering if

  • 0

I was using set_intersection from the STL in C++, and I was wondering if there was any rule on which set would be used when creating the intersection. Or is the behavior undefined, and potentially implementation dependent.

On linux using g++ (ver. 4.4.6), it seems that it always uses the first set that is passed into the set_difference function, but I’m not sure I can rely on this. As you can see from the example below, there is other data in each set member that is not considered in the operator< function.

#include <set>
#include <algorithm>
#include <iostream>
using namespace std;

class myClass {
  public:
    myClass(int val, int data)
        : value(val),
          metaData(data) {}

    // Only consider the value, not metaData
    bool operator<(const myClass &other) const{
        return value < other.value;
    }

    void print() const {
        cout << "Value: " << value << " metaData: " << metaData << endl;
    }

  private:
    int value;
    int metaData;

};

int main() {
    // Create two sets with some data
    set<myClass> set1;
    set<myClass> set2;
    set<myClass> intersect;

    // Set1 has 1, 2, 3, 4
    set1.insert(myClass(1,-10));
    set1.insert(myClass(2,-10));
    set1.insert(myClass(3,-10));
    set1.insert(myClass(4,-10));

    // Set2 has -1, 2, 3
    set2.insert(myClass(-1, 10));
    set2.insert(myClass(2, 10));
    set2.insert(myClass(3, 10));

    set_intersection(set1.begin(), set1.end(),
                   set2.begin(), set2.end(),
                   inserter(intersect, intersect.begin()));

    for_each(intersect.begin(), intersect.end(),
             mem_fun_ref(&myClass::print));

}
// The output of this code is 
// Value: 2 metaData: -10
// Value: 3 metaData: -10
  • 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-27T18:04:58+00:00Added an answer on May 27, 2026 at 6:04 pm

    Assuming you’re asking about set_intersection as in the question title, rather than set_difference as in the code sample, the C++ standard is explicit:

    C++03 §25.3.5.3[lib.set.intersection]/5

    if an element is present in both ranges, the one from the first range is copied

    Current standard makes this point even stronger, mostly for the sake of multisets which are now stable with regards to the order of equivalent keys:

    C++11 §25.4.5.3[set.intersection]/5

    If [first1,last1) contains m elements that are equivalent to each other and [first2,
    last2)
    contains n elements that are equivalent to them, the first min(m, n) elements shall be copied from the first range to the output range, in order.

    As for set_difference, it simply copies the elements of the first sorted sequence that are not present in the second.

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

Sidebar

Related Questions

Are there constant-time algorithms for binary set intersection and union? I imagine using bitmaps
I have a parent class which contains a child object. I am using set
Is there any way to stop the execution of a matlab program from the
I have a set of edges detected from an image using edge detector of
I'm intersecting a set of 100,000 numbers and a set of 1,000 numbers using
I am using: set constraints all deferred; (lots of deletes and inserts) commit; This
I'm executing stored procedures using SET FMTONLY ON, in order to emulate what our
What is the benefit of using SET XACT_ABORT ON in a stored procedure?
In our application we are using set of images as slides. If we click
In plain English, what are the disadvantages and advantages of using SET TRANSACTION ISOLATION

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.