C++ Sets and Their Types

Posted on 2007-09-28
Last Modified: 2013-12-14
I am new to C++, and I am writing a breath first search.  I would like to use a set to prevent from adding elements to a data structure that are already contained in that data structure.  Thus I know a set is what I want.  But besides that I understand sets in C++ yet.

I am having trouble figuring out what types a set can take on.  Assume set is included as well as Vertex.h...

std::set<int> mySet;
// This works!

std::set<Vertex> mySet;
Vertex v;
// This Doesn't Work!

Can sets only take on primitive data types.  Can you make a set hold an object type other than primitive objects?
Question by:323k13l
    LVL 4

    Accepted Solution

    You have to choose a sorting criterion. By default, set uses the less-than operator. Primitive types can be directly compared using < but classes can't unless you define operator< in your class:

    bool operator<(const Vertex& right) const;

    Another option is to define a sorting criterion outside of the class and pass it as a template argument.

    class vertexSortCriterion {
        bool operator() ( const Vertex& v1, const Vertex& v2 );

    std::set<Vertex,vertexSortCriterion> mySet;

    Author Comment

    I tried to apply what you said but I think I am applying it wrong.  I have a Vertex.h where methods are declared, and I have a Vertex.cpp where the methods are implemented.  It the header file I put:

      bool operator<(const Vertex& right) const

    In the source file I put:

    bool operator<(const Vertex& right) const {
      return ._name < right._name;                             // _name is a int class-variable

    What am I doing wrong here?

    Author Comment

    Nevermind, the only reason the above code did not work was because I left off the semicolon at the end of the declaration in the first code segment.  Thank you for your help, Ichijo.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deletiā€¦
    Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now