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

  • Home
  • SEARCH
  • 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 6804371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:27:27+00:00 2026-05-26T19:27:27+00:00

So, I’m trying to check whether a particular object is already present in the

  • 0

So, I’m trying to check whether a particular object is already present in the set. For this, I use the count() method. Now, it doesn’t seem to return the right answer. Let me explain the problem a bit more clearly —

I have declared a class this way

class Node{
        public:
                Node(int _state=0, int _cost=0)
                {
                        state = _state;
                        cost = _cost;
                }

                bool operator<(const Node& rhs)
                {
                        return cost < rhs.cost;
                }

                bool operator==(const Node& rhs)
                {
                        cout << "== operator method used" << endl;
                        if (rhs.state == state)
                                return true;
                        return false;
                }

                int state;
                int cost;
};

in my code, I declare a set like this —

set<Node*> myset;

after a few insertions, myset is like this {{1, 5}, {2, 6}, {3, 9}}

now I check whether {1, 7} is part of the set. How would it do it? I have written a operator== method in Node class, which is never called. Then on what basis does count() check if the object is already in the set?… I would want the count to work in a fashion that if {1, 5} is already there in myset, it should view {1, 7} as a duplicate entry.

  • 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-26T19:27:28+00:00Added an answer on May 26, 2026 at 7:27 pm

    how does count in c++ stl set work?

    It uses operator< by default.

    In fact, in general, C++ Standard Library containers use !(a < b) && !(b < a) to determine the property of equivalence.

    You can override the comparator used to perform this check by providing your own Compare template argument to the container type, though there is rarely a reason to — you should usually simply define operator< for your type instead, as you have done. (Make sure that it creates a Strict Weak Ordering, though.)

    in my code, I declare a set like this —

    set<Node*> myset;
    

    after a few insertions, myset is like this {{1, 5}, {2, 6}, {3, 9}}

    No, your set is never like this. Your set contains pointers, not Nodes. Make it a set<Node> instead.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I want use html5's new tag to play a wav file (currently only supported

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.