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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:59:26+00:00 2026-05-16T16:59:26+00:00

For example, I want to write overload functions for set_difference which compares the type

  • 0

For example, I want to write overload functions for set_difference which compares the type std::set<point>

class myIter : public std::iterator<std::input_iterator_tag, int> {
public:
    myIter(int n) : num(n){}
    myIter(const myIter & n) : num(n.num){}
    int & operator *(){return num;}
    myIter & operator ++(){++num; return *this;}
    bool operator !=(const myIter & n){return n.num != num;}
private:
    int num;
};

struct point
{
  point(int X, int Y):x(X), y(Y){}
  int x;
  int y;
}

int main()
{
   set <point> myset;
   myset.insert(point(1, 1);
   myset.insert(point(3, 2);
   myset.insert(point(5, 3);

  //find the missing elements in set for `point.x` using `set_difference`

   std::set<int> missing;

   std::set_difference(myIter(myset.begin()->x+1), myIter(myset.rbegin()->x),
   myset.begin(), myset.end(), std::insert_iterator<std::set<int>>(missing, missing.begin()));

}

After applying the std::set_difference on point.x variables the set<int> missing must be:

missing[0] {2}
missing[1] {4}

How do i know how to write the overload operators for the operation?

  • 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-16T16:59:26+00:00Added an answer on May 16, 2026 at 4:59 pm

    Firstly, std::set<point> requires that point is less-than-comparable. You can either define operator< for point, or provide a separate function object that does the comparison as the second template parameter std::set<point,MyCompare>.

    Once you actually have your elements in the set, you can use set_difference. It’s worth noting that set_difference doesn’t actually require you to use std::set for your input — you could just use a vector and thus avoid having to provide the comparison function.

    To use set_difference, you will need to ensure that the value_types of the two iterator ranges are the same, so you’ll need another iterator wrapper that returns just the x portion of the point values for the second range.

    std::set_difference(myIter(myset.begin()->x+1), myIter(myset.rbegin()->x),
        extractXIter(myset.begin()), extractXIter(myset.end()),
        std::insert_iterator<std::set<int>>(missing, missing.begin()));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to overload methods of comparison for a personnal class. For example if
For example, I want to write an activity that launches when you return from
I want to create URL re-write rules for following URL in htaccess. http://example.com/vedios/category/fun/ ->
I want to write a java program to calculate integral with three-point Gauss. How
I have two table like this job(id,title,location,...) job_page(id,id_job,id_page,page_name) For example I want to write
I want to create a class that has several public variables and methods,but behaves
I have two XML examples that I want to write a schema for: Example
I have a signed applet and I want to write out dll files which
i would like to know how can i write a class delegate and overload
I want to write something like =HYPERLINK(http://example.com; Example) to a comma-separated CSV file, but

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.