Thread: STL set of structs

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    34

    STL set of structs

    Hi Everybody. If any of you know how to make an STL set of classes or structs, that would be cool. Which operators do you need to implements so if I have myclass, I can have an stl set of type myclass. Examples are welcome too.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You need a way to compare instances of your class. One way is an operator<, which makes sense if there is one obvious way to compare two class objects. Another option is to write a comparison class whose operator() compares two class instances like a global operator< would. If you use that then you have to specify it when you declare your set. If you just use operator< then the set will automatically use that for its sorting.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    First method as described by Daved...
    Code:
    #include <set>
    ...
    
    struct foo
    {
        int bar;
    };
    
    bool operator<(const foo& left, const foo& right)
    {
        return left.bar < right.bar;
    }
    
    int main()
    {
        std::set<foo> fooset;  // Uses operator< automatically for sorting items in the container
    
        ...
    
        return 0;
    }
    Or, second method as described above...
    Code:
    #include <set>
    ...
    
    struct foo
    {
        int bar;
    };
    
    struct comp
    {
        inline bool operator()(const foo& left,const foo& right)
        {
            return left.bar < right.bar;
        }
    };
    
    int main()
    {
        std::set<foo,comp> fooset;  // Uses comparison struct/class object comp to sort the container
    
        ...
    
        return 0;
    }
    If the data member you are comparing happens to be private then you'll need to make adjustments, either declaring operator< as a friend to the class/struct if you use that method, or creating accessor functions for returning the value of those private data members so they can be compared.

    [edit]Yay, my 2000th post, I thought for sure I was still around 1997 or so...[/edit]
    Last edited by hk_mp5kpdw; 07-16-2007 at 06:10 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    To be pedantic: a predicate like comp should not change its state after a call or a copy. After all, if a and b do not change, then (a < b) should likewise not change. As such, it would be better to write:
    Code:
    struct comp
    {
        inline bool operator()(const foo& left,const foo& right) const
        {
            return left.bar < right.bar;
        }
    };
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Set STL
    By DarkDot in forum C++ Programming
    Replies: 5
    Last Post: 05-02-2007, 02:58 PM
  2. STL Set erase()
    By R.Stiltskin in forum C++ Programming
    Replies: 11
    Last Post: 03-27-2007, 08:09 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. Find all possible subset of a given set
    By Downnin in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 02:03 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM