Link to home
Create AccountLog in
Avatar of 323k13l
323k13l

asked on

C++ Sets and Their Types

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;
mySet.insert(10);
// This works!

std::set<Vertex> mySet;
Vertex v;
mySet.insert(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?
ASKER CERTIFIED SOLUTION
Avatar of Ichijo
Ichijo

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of 323k13l
323k13l

ASKER

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?
Avatar of 323k13l

ASKER

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.