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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:05:29+00:00 2026-06-14T22:05:29+00:00

i am having trouble to understand using find and erase with STL set. I

  • 0

i am having trouble to understand using find and erase with STL set. I have this:

struct NotMemberFound : exception {};

class FriendGroup
{
private:
    set<Person, ClassComp> members;
public:
    FriendGroup(): {}

    virtual void add_member(const Person& person)
    {
        members.insert(person);
        n_members = members.size();
    }

    virtual void remove_member(const Person& person)
    {
        const set<Person, ClassComp>::iterator it = members.find(person);

        if (it == members.end())
            throw NotMemberFound();
        else
        {
            members.erase(it);
        }
    }
}

Person have one attribute (String Name) and the metod get_name() that return his only attribute. And ClassComp its define like this:

struct ClassComp
{
    bool operator() (const Person& a, const Person& b) const
    {
        return a.get_name() != b.get_name();
    }
};

Well, when i am trying to use it like this:

int main(void)
{
    Person messi("Messi");
    Person xavi("Xavi");
    Person iniesta("Iniesta");

    FriendGroup barcelona;

    barcelona.add_member(messi);
    barcelona.add_member(xavi);
    barcelona.add_member(iniesta);

    barcelona.remove_member(iniesta);

    return 0;
}

The program throw an exception (NotMemberFound) when i call remove_member with the last element added. But if i try to erase any other member i don’t have this problem.

What is wrong with my code?

// Sorry for my horrible English :S //

  • 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-14T22:05:30+00:00Added an answer on June 14, 2026 at 10:05 pm

    Your comparator should be

    struct ClassComp {
        bool operator() (const Person& a, const Person& b) const {
            return a.get_name() < b.get_name();
        }
    };
    

    The comparator should say if an element is inferior to another. In your case, it’s like you said to the std::set that messi < xavi and that xavi < messi. When looking for an element, it cannot find it because it assumes they are sorted and it looks for them using binary search.

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

Sidebar

Related Questions

I am having trouble getting any find operations to work using the SQL function
I'm having trouble using the regex of the find command. Probably something I don't
I have read a couple of articles but im still having trouble understanding this.
I'm having trouble using the GWT-Grails plugin. I tried to follow this example exactly,
I'm having trouble setting 2 positions on a scroll function using offset. I have
I'm having trouble using the layout manager system with Qt. This is going to
I'm having trouble trying to understand how to use recursion with this problem. I'm
I have created some rounded navigation tabs using CSS and am having trouble when
I am having trouble understand a key concept of Symfony 2. I am working
I'm having terrible trouble trying to understand python scoping rules. With the following script:

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.