Practical C Programming, 3rd Edition

I l @ ve RuBoard

Suppose we have more students who want to take a class than we have places to put them. In that case we'll have to start a waiting list for those people who want to get in if space opens up.

We can't use a set as a waiting list, because a set is unordered and we want to make our list first come, first served . For that we need the STL container std::list . A std::list is an ordered list of elements that allows us to add students on the back end and remove them from the front end.

Our waiting list declaration is:

#include <list> std::list<std::string> waiting_list; // Waiting list for the class

When we want to add a student to the back of the list, we use the code:

waiting_list.push_back(student);

To remove a student from the front, we need two statements:

student = waiting_list.top( ); waiting_list.pop_front( );

Iterating through a list is just like iterating through a set . That's because they are both containers, and the STL is designed so that all containers act the same whenever possible.

I l @ ve RuBoard

Категории