Thread: set Insert the customizedCLass not working as expected with pointer

  1. #1
    Registered User
    Join Date
    Nov 2016
    Posts
    26

    set Insert the customizedCLass not working as expected with pointer

    Hi,

    I have a class(Territory) which has two fields string(name) and int(ID).
    set<Territory>setTerritories ////Insertion is as expected.It isn't insert duplicate
    [code]set<Territory*>setTerritories; [code] //Insertion is not as expected. This inserts duplicate

    i have a operator overloading to insert the customized class into the set.But it isn't called for the set<Territory*>. I am missing something here.but not sure.
    Code:
    bool operator<(const Territory& rhs) const {
            if (this->m_strName < rhs.m_strName)
            {
                return true;
            }
            return false;
        }
    so please find my running code in the attachment and guide me .

    “Truth can only be found in one place: the code.”
    Attached Files Attached Files

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need a comparator that compares two Territory* and use it in the set. Overloading operator< only makes the comparison for two Territory objects, not pointers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2016
    Posts
    26
    Thank you.It's working now

    Code:
    struct customCompare {
         bool operator()(const Territory* lhs, const Territory* rhs)const {
            return lhs->getTerritoryId() < rhs->getTerritoryId();
        }        
    };
    
        {
            set<Territory*,customCompare>setTerritories;
    
            std:pair<set<Territory*>::iterator, bool> insertStatus;
    
            insertStatus = setTerritories.insert(new Territory("Ajanta", 3));
            if (insertStatus.second == false)
            {
                cout << "Insertion new Territory("Ajanta",3) failed" << endl;
            }
    
            insertStatus = setTerritories.insert(new Territory("Ellora", 5));
            if (insertStatus.second == false)
            {
                cout << "Insertion new Territory("Ellora",5) failed" << endl;
            }
            insertStatus = setTerritories.insert(new Territory("Ajanta", 3));
            if (insertStatus.second == false)
            {
                cout << "Insertion new Territory("Ajanta",3) failed" << endl;
            }
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Set insert not working
    By Ducky in forum C++ Programming
    Replies: 3
    Last Post: 07-26-2014, 10:17 AM
  2. && not working as expected
    By TonyBalony in forum C Programming
    Replies: 4
    Last Post: 12-14-2011, 12:30 PM
  3. Map insert not working?
    By JMK in forum C++ Programming
    Replies: 7
    Last Post: 09-28-2010, 01:20 AM
  4. addrecord.c:45: error: expected ')' before 'INSERT'
    By pinky in forum C Programming
    Replies: 3
    Last Post: 10-15-2009, 08:53 AM
  5. cin.get(); doesn't seem to be working as expected
    By Diablo84 in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2005, 07:00 PM

Tags for this Thread