saneman said:
In std::set key_type = value_type. So if I want to find a value in a
std::set I do:
find(k)
where k is the value that I want to find...but I supply find with the
data I would like to find...makes no sense to me since I already have
the data, k, that I want to find.
One does not usually some_set.find(k) to get access to k, but to
determine whether k is a member of the set (which has some
programmer-determined meaning), or to get an iterator to its position.
You can check the membership with getting the iterator via the count(k)
method.
<begin quick example>
Suppose you have some nodes. At any given moment, some are red, while
the rest are black. How do you keep track of which nodes are red and
which are black? One way is to keep a set of red_nodes. Whenever a
node becomes red, insert it into the set; whenever a node becomes black,
erase it from the set. Whenever you need to determine whether a node is
red or black, you can just check whether it is in the set. Further, you
have a ready collection of all the red nodes, if you're into that sort
of thing. In fact, if that sounds useful, you might consider keeping a
separate set of black_nodes as well, just so you can iterate over them.
<end quick example; begin unnecessarily elaborate example, offering no
new information>
Suppose you're an elderly school teacher, and your memory is starting to
go (but at least you have tenure :-/ ). One day, a student hands in
homework late, but you can't remember whether you like that student
enough to accept the tardy assignment. At any given moment, a few of
your students are your favorites, but is this particular student a
current favorite? What to do?
Fortunately, you have a long-running server-side program with a nifty
web interface that lets you track information about your students. You
hit the emacs key-chord that pulls up the appropriate web page, and
enter the student's name ("What's your name again, dear?"). The
server-side program immediately returns either a big green "Yes," or an
angry red "No," letting you know whether to be merciful or vengeful
(respectively).
How does the server-side program remember which students are your
favorites? It maintains a set, defined something like this:
typedef std::string student_name;
std::set<student_name> pet_set;
pet_set pets;
When you enter a student's name (the same way every time, since
correctness and consistency are fortunate hobgoblins of your aging
mind), the server-side program just checks for set membership:
typedef std::string mercy_status;
student_name name = http_request.get_parameter("name");
mercy_status result = pets.count(name) ? "Green" : "Red";
Very nice! Now just take the kid's homework, and insist that an apple
be on your desk the following morning. OK?
<end elaborate example>