However, because this remove() is not a member, it can’t adjust the size of the list. Instead, it makes sure all the nonremoved items are at the beginning of the list, and it returns an iterator to the new past-the-end value. You can then use this iterator to fix the list size. For example, you can use the list erase() method to remove a range that describes the part of the list that is no longer needed. Listing 16.18 shows how this process works.

Listing 16.18. listrmv.cpp

// listrmv.cpp -- applying the STL to a string

#include

#include

#include

void Show(int);

const int LIM = 10;

int main()

{

    using namespace std;

    int ar[LIM] = {4, 5, 4, 2, 2, 3, 4, 8, 1, 4};

    list la(ar, ar + LIM);

    list lb(la);

    cout << "Original list contents:\n\t";

    for_each(la.begin(), la.end(), Show);

    cout << endl;

    la.remove(4);

    cout << "After using the remove() method:\n";

    cout << "la:\t";

    for_each(la.begin(), la.end(), Show);

    cout << endl;

    list::iterator last;

    last = remove(lb.begin(), lb.end(), 4);

    cout << "After using the remove() function:\n";

    cout << "lb:\t";

    for_each(lb.begin(), lb.end(), Show);

    cout << endl;

    lb.erase(last, lb.end());

    cout << "After using the erase() method:\n";

    cout << "lb:\t";

    for_each(lb.begin(), lb.end(), Show);

    cout << endl;

    return 0;

}

void Show(int v)

{

    std::cout << v << ' ';

}

Here’s the output of the program in Listing 16.18:

Original list contents:

    4 5 4 2 2 3 4 8 1 4

After using the remove() method:

la: 5 2 2 3 8 1

After using the remove() function:

lb: 5 2 2 3 8 1 4 8 1 4

After using the erase() method:

lb: 5 2 2 3 8 1

As you can see, the remove() method reduces the list la from 10 elements to 6 elements. However, list lb still contains 10 elements after the remove() function is applied to it. The last 4 elements are disposable because each is either the value 4 or a duplicate of a value moved farther to the front of the list.

Although the methods are usually better suited, the nonmethod functions are more general. As you’ve seen, you can use them on arrays and string objects as well as STL containers, and you can use them with mixed container types—for example, to save data from a vector container to a list or a set.

Using the STL

The STL is a library whose parts are designed to work together. The STL components are tools, but they are also building blocks to create other tools. Let’s illustrate this with an example. Suppose you want to write a program that lets the user enter words. At the end, you’d like a record of the words as they were entered, an alphabetical list of the words used (capitalization differences ignored), and a record of how many times each word was entered. To keep things simple, let’s assume that the input contains no numbers or punctuation.

Entering and saving the list of words is simple enough. Following the example of Listings 16.8 and 16.9, you can create a vector object and use push_back() to add input words to the vector:

vector words;

string input;

while (cin >> input && input != "quit")

    words.push_back(input);

What about getting the alphabetic word list? You can use sort() followed by unique(), but that approach overwrites the original data because sort() is an in-place algorithm. There is an easier way that avoids this problem. You can create a set object and copy (using an insert iterator) the words from the vector to the set. A set automatically sorts its contents, which means you don’t have to call sort(), and a set allows only one copy of a key, so that takes the place of calling unique(). Wait! The specification called for ignoring the case differences. One way to handle that is to use transform() instead of copy() to copy data from the vector to the set. For the transformation function, you can use one that converts a string to lowercase:

set wordset;

transform(words.begin(), words.end(),

    insert_iterator > (wordset, wordset.begin()), ToLower);

The ToLower() function is easy to write. You just use transform() to apply the tolower() function to each element in the string, using the string both as source and destination. Remember, string objects, too, can use the STL functions. Passing and returning the string as a reference means the algorithm works on the original string without having to make copies. Here’s the code for ToLower():

string & ToLower(string & st)

{

    transform(st.begin(), st.end(), st.begin(), tolower);

    return st;

}

Перейти на страницу:

Все книги серии Developer's Library

Похожие книги