Section : Using the STL .cuny.edu



Section : Using the STL

The STL is a library whose parts are designed to work together. The STL components are tools, but they also are building blocks to create other tools. Let's illustrate that 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, assume the input contains no numbers or punctuation.

Entering and saving the list of words is simple enough. Following the example of Listing 16.5, 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. Create a set object, and copy (using an insert iterator) the words from the vector to the set. A set automatically sorts its contents, taking the place of calling sort(), and a set only allows 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, 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. 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.

string & ToLower(string & st)

{

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

return st;

}

One possible problem is that the tolower() function is defined as int tolower(int), and some compilers want the function to match the element type, which is char. One solution is to replace tolower with toLower and to provide the following definition:

char toLower(char ch) { return tolower(ch); }

To get the number of times each word appeared in the input, you can use the count() function. It takes a range and a value as arguments and returns the number of times the value appears in the range. You can use the vector object to provide the range and the set object to provide the list of words to count. That is, for each word in the set, count how many times it appears in the vector. To keep the resulting count associated with the correct word, store the word and the count as a pair object in a map object. The word will be the key (just one copy), and the count will be the value. This can be done in a single loop:

map wordmap;

set::iterator si;

for (si = wordset.begin(); si != wordset.end(); si++)

wordmap.insert(pair(*si, count(words.begin(),

words.end(), *si)));

[pic]

Older STL implementations declare count() as type void. Instead of using a return value, you provide a fourth argument passed as a reference, and the number of items is added to that argument:

int ct = 0;

count(words.begin(), words.end(), *si), ct)); count added to ct

The map class has an interesting feature—you can use array notation with keys serving as indices to access the stored values. For example, wordmap["the"] would represent the value associated with the key "the", which in this case is the number of occurrences of the string "the". Because the wordset container holds all the keys used by wordmap, you can use the following code as an alternative and more attractive way of storing results:

for (si = wordset.begin(); si != wordset.end(); si++)

wordmap[*si] = count(words.begin(), words.end(), *si);

Because si points to a string in the wordset container, *si is a string and can serve as a key for wordmap. This code places both keys and values into the wordmap map.

Similarly, you can use the array notation to report results:

for (si = wordset.begin(); si != wordset.end(); si++)

cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download