25.2 Class ListA Set of Students

I l @ ve RuBoard

25.2 Class List”A Set of Students

Suppose we are working on a school scheduling program. We need to keep track of the number of students in each class. In STL terms, we need a container to hold the students. The type of container we need is a set .

A set is a container that holds an ordered list of items. In this case our items are student names (which are represented by strings). To define our class list, we use the following declarations:

 #include <set> #include <string> std::set<std::string> class_set;      // A list of students in the class 

The statement #include <set> is used to get the definition of the set template. We then use this template to define a set of strings named class_set . (The term "set" is used instead of the more common "list" to avoid confusion with the STL type list , which is discussed later in this chapter.)

We can now add names to our set of students:

 while (! in_file.eof()) {        std::string student;                in_file >> student;        class_set.insert(student);     } 

This code uses the member function insert to add new students to our set.

25.2.1 Iterating Through a Set

Now we need to print out the class roster. To do so, we need to write some code to go through the set of students and print out each student's name . The STL has a concept called an iterator to help us do just that. You can think of an iterator as a pointer to an item in a set (or any other STL container). Actually, an iterator is much more complex than a pointer, but the STL tries to hide that complexity from you and make an iterator look as much like a pointer as possible.

Our iterator for the printing of students is declared as follows :

 std::set<std::string>::const_iterator cur_student; 

We are using const_iterator because we will not be changing the values of the items as we go through them.

Now for the actual stepping through the set. The first element of the set is called class_set.begin() ; the empty space past the last element is called class_set.end() . To step through all the elements of the set, we use the following code:

 for (cur_student = class_set.begin();           cur_student != class_set.end();         ++cur_student)     {         std::cout << (*cur_student) << '\n';     } 

Notice that to end the loop we used the condition:

 cur_student != class_set.end(); 

instead of:

 cur_student < class_set.end();     // Wrong! 

That's because the elements of a set are unordered, which means that you cannot compare two iterators for anything except equality. They either point to the same element or they don't. It's meaningless to say that one element comes before another.

25.2.2 Using std::foreach to Write Out the Set

Iterating through an entire container is a common operation. In fact, there's an STL library function to do just that. Rather than write our own code, we can use the STL foreach algorithm to output our list:

 #include <algorithem> //...... static void write_student(set<string>::const_iterator& cur_student) {     std::cout << *cur_student << '\n'; } //......     foreach(class_set.begin(), class_set.end(), write_student); 

The include file algorithm brings in the STL algorithm functions. In this case, the algorithm we are interested in is foreach . The function, write_student , merely writes out a student's name.

The heart of the code is the foreach function call. The first argument is the place to start; the second argument is one after the place to stop. (Note that this one-past-stopping is a key concept used in many STL functions.) Finally, we have a function to be called for each student.

25.2.3 Multisets

One of the problems [1] with setsis that each item in a set must be unique. So what happens if two students named John Smith enroll in the same class? When we try to do our insert , the set code will see that we've already got John Smith in the set and refuse to add another one.

[1] I've been told this is a feature, not a problem.

A set of objects that can contain duplicates is called a std::multiset . Except for the way in which it handles duplicates, a std::multiset is just like a set . So we really need to declare our class using this new container:

 std::multiset<string> class_set;      // A list of students in the class 
I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net