Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Professional C++ [eng].pdf
Скачиваний:
284
Добавлен:
16.08.2013
Размер:
11.09 Mб
Скачать

Chapter 22

The implementation of the function-call operator simply uses find() to search for the name parameter in the mNames list of strings, returning true if find() returns a valid iterator, and false if it returns the end iterator.

//

//function-call operator for NameInList functor

//Returns true if it can find name in mNames, false otherwise.

//Uses find() algorithm.

//

bool NameInList::operator() (const string& name)

{

return (find(mNames.begin(), mNames.end(), name) != mNames.end());

}

Testing the auditVoterRolls() Function

That’s the complete implementation of the voter roll audit functionality. Here is a small test program:

#include <algorithm> #include <functional> #include <map> #include <list> #include <iostream> #include <utility> #include <string> using namespace std;

void printString(const string& str)

{

cout << “ {“ << str << “}”;

}

void printCounty(const pair<const string, list<string> >& county)

{

cout << county.first << “:”;

for_each(county.second.begin(), county.second.end(), &printString); cout << endl;

}

int main(int argc, char** argv)

{

map<string, list<string> > voters; list<string> nameList, felons; nameList.push_back(“Amy Aardvark”); nameList.push_back(“Bob Buffalo”); nameList.push_back(“Charles Cat”); nameList.push_back(“Dwayne Dog”);

voters.insert(make_pair(“Orange”, nameList));

nameList.clear(); nameList.push_back(“Elizabeth Elephant”);

652

Mastering STL Algorithms and Function Objects

nameList.push_back(“Fred Flamingo”); nameList.push_back(“Amy Aardvark”);

voters.insert(make_pair(“Los Angeles”, nameList));

nameList.clear(); nameList.push_back(“George Goose”); nameList.push_back(“Heidi Hen”); nameList.push_back(“Fred Flamingo”);

voters.insert(make_pair(“San Diego”, nameList));

felons.push_back(“Bob Buffalo”); felons.push_back(“Charles Cat”);

for_each(voters.begin(), voters.end(), &printCounty); cout << endl;

auditVoterRolls(voters, felons); for_each(voters.begin(), voters.end(), &printCounty);

return (0);

}

The output of the program is:

Los Angeles: {Elizabeth Elephant} {Fred Flamingo} {Amy Aardvark}

Orange: {Amy Aardvark} {Bob Buffalo} {Charles Cat} {Dwayne Dog}

San Diego: {George Goose} {Heidi Hen} {Fred Flamingo}

Los Angeles: {Elizabeth Elephant}

Orange: {Dwayne Dog}

San Diego: {George Goose} {Heidi Hen}

Summar y

This chapter concludes the basic STL functionality. It provided an overview of the various algorithms and function objects available for your use, and showed you how to write your own function objects. We hope that you have gained an appreciation for the usefulness of the STL containers, algorithms, and function objects. If not, think for a moment about rewriting the voter registration audit example without the STL. You would need to write your own linked-list and map classes, and your own searching, removing, finding, iterating, and other algorithms. The program would be much longer, harder to debug, and more difficult to maintain.

If you aren’t impressed by the algorithms and function objects, or find them too complex, you obviously don’t have to use them. Feel free to pick and choose as well: if the find() algorithm fits perfectly in your program, don’t eschew it just because you aren’t using the other algorithms. Also, don’t take the STL as an all-or-nothing proposition. If you want to use only the vector container and nothing else, that’s fine too.

Chapter 23 continues the STL topic with some advanced features, including allocators, iterator adapters, and writing your own algorithms, containers, and iterators.

653

Customizing and

Extending the STL

The previous two chapters have shown that the STL is a powerful general-purpose collection of containers and algorithms. The information covered so far should be sufficient for most applications. The STL, however, is much more flexible and extensible than the previous chapters have demonstrated. For example, you can apply iterators to input and output streams; write your own containers, algorithms, and iterators; and even specify your own memory allocation schemes for containers to use. This chapter provides a taste of these advanced features, primarily through the development of a new STL container: the hashmap. The specific contents of the chapter include:

A closer look at allocators

Iterator adapters

Extending the STL

Writing algorithms

Writing containers: a hash map implementation

Writing Writing iterators: a hash map iterator implementation

This chapter is not for the faint of heart! The contents delve into some of the most complicated and syntactically confusing areas of the C++ language. If you’re happy with the basic STL containers and algorithms from the previous two chapters, you can skip this one. However, if you really want to understand the STL, not just use it, give this chapter a chance. Make sure that you’re comfortable with the template material in Chapter 11 before reading this chapter.