Online Book Reader

Home Category

Professional C__ - Marc Gregoire [202]

By Root 1296 0
bs.

list::iterator it;

for (it = dictionary.begin(); it != dictionary.end(); ++it) {

if (*it > *iterLastB)

break;

}

// Add in the bwords. This action removes the elements from bWords.

dictionary.splice(it, bWords);

}

// print out the dictionary

for (auto it = dictionary.cbegin(); it != dictionary.cend(); ++it) {

cout << *it << endl;

}

Code snippet from ListSplice\ListSplice.cpp

The result from running this program looks like this:

aardvark

ambulance

bathos

balderdash

canticle

consumerism

There are also two other forms of splice(): one that inserts a single element from another list and one that inserts a range from another list. See the Standard Library Reference resource on the website for details.

Splicing is destructive to the list passed as a parameter: It removes the spliced elements from one list in order to insert them into the other.

More Efficient Versions of Algorithms

In addition to splice(), the list class provides special implementations of several of the generic STL algorithms. The generic forms are covered in Chapter 13. Here we discuss only the specific versions provided by list.

When you have a choice, use the list methods rather than the generic STL algorithms because the former are more efficient.

The following table summarizes the algorithms for which list provides special implementations as methods. See the Standard Library Reference resource on the website and Chapter 13 for prototypes, details on the algorithms, and their specific running time when called on list.

METHOD DESCRIPTION

remove()

remove_if() Removes certain elements from the list.

unique() Removes duplicate consecutive elements from the list, based on operator== or a user supplied binary predicate.

merge() Merges two lists. Both lists must be sorted to start, according to operator< or a user defined comparator. Like splice(), merge() is destructive to the list passed as an argument.

sort() Performs a stable sort on elements in the list.

reverse() Reverses the order of the elements in the list.

The following section demonstrates most of these methods.

list Example: Determining Enrollment

Suppose that you are writing a computer registration system for a university. One feature you might provide is the ability to generate a complete list of enrolled students in the university from lists of the students in each class. For the sake of this example, assume that you must write only a single function that takes a vector of lists of student names (as strings), plus a list of students that have been dropped from their courses because they failed to pay tuition. This method should generate a complete list of all the students in all the courses, without any duplicates, and without those students who have been dropped. Note that students might be in more than one course.

Here is the code for this method, with comments explaining the code. With the power of STL lists, the method is practically shorter than its written description! Note that the STL allows you to “nest” containers: In this case, you can use a vector of lists.

// courseStudents is a vector of lists, one for each course. The lists

// contain the students enrolled in those courses. They are not sorted.

//

// droppedStudents is a list of students who failed to pay their

// tuition and so were dropped from their courses.

//

// The function returns a list of every enrolled (non-dropped) student in

// all the courses.

list

getTotalEnrollment(const vector>& courseStudents,

const list& droppedStudents)

{

list allStudents;

// Concatenate all the course lists onto the master list

for (auto& lst : courseStudents) {

allStudents.insert(allStudents.end(), lst.begin(), lst.end());

}

// Sort the master list

allStudents.sort();

// Remove duplicate student names (those who are in multiple courses).

allStudents.unique();

// Remove students who are on the dropped list.

// Iterate through the drop list, calling remove on the

// master list for each student in the dropped list.

for (auto& str

Return Main Page Previous Page Next Page

®Online Book Reader