What is the point with std::set?

S

saneman

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.
 
I

Ian Collins

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.

find returns an iterator giving you the position of the item in the set.
 
K

Kai-Uwe Bux

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.

If find(k) returns end(), you find out that the set did not contain k.


Best

Kai-Uwe Bux
 
P

peter koch

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.

Adding to what others have told you, the set-element might also
contain data that is not part of the comparison. This data could be
useful!

/Peter
 
D

dave_mikesell

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.

But it's useful if you want to determine if an object or value belongs
to a set of objects or values.
 
J

Juha Nieminen

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.

std::set has a multitude of useful uses.

Example: Read all the words from standard input, and output all the
unique words found, in alphabetical order.
Not only does std::set make this laughably easy and fast, it's also
memory-efficient if there are lots of repeated words in the input (every
unique word is stored in memory only once).
 
J

Jeff Schwab

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>
 
B

bytebro

<begin quick example>

<snip>

<end quick example; begin unnecessarily elaborate example, offering no
new information>

<snip>

<end elaborate example>

Outstanding! If only my tutor had given such clear, concise, erudite,
and witty examples when I was at college, I think I'd probably have
graduated in half the time I did.

Nicely done, sir.
 
G

Gerhard Fiedler

find returns an iterator giving you the position of the item in the set.

.... and that allows you, for example, to easily get the item that follows k
in the set.

Gerhard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top