Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Thinking In C++, 2nd Edition, Volume 2 Standard Libraries& Advanced Topics - Eckel B..pdf
Скачиваний:
318
Добавлен:
24.05.2014
Размер:
2.09 Mб
Скачать

5: STL Algorithms

The other half of the STL is the algorithms, which are templatized functions designed to work with the containers (or, as you will see, anything that can behave like a container, including arrays and string objects).

The STL was originally designed around the algorithms. The goal was that you use algorithms for almost every piece of code that you write. In this sense it was a bit of an experiment, and only time will tell how well it works. The real test will be in how easy or difficult it is for the average programmer to adapt. At the end of this chapter you’ll be able to decide for yourself whether you find the algorithms addictive or too confusing to remember. If you’re like me, you’ll resist them at first but then tend to use them more and more.

Before you make your judgment, however, there’s one other thing to consider. The STL algorithms provide a vocabulary with which to describe solutions. That is, once you become familiar with the algorithms you’ll have a new set of words with which to discuss what you’re doing, and these words are at a higher level than what you’ve had before. You don’t have to say “this loop moves through and assigns from here to there … oh, I see, it’s copying!” Instead, you say copy( ). This is the kind of thing we’ve been doing in computer programming from the beginning – creating more dense ways to express what we’re doing and spending less time saying how we’re doing it. Whether the STL algorithms and generic programming are a great success in accomplishing this remains to be seen, but that is certainly the objective.

Function objects

A concept that is used heavily in the STL algorithms is the function object, which was introduced in the previous chapter. A function object has an overloaded operator( ), and the result is that a template function can’t tell whether you’ve handed it a pointer to a function or an object that has an operator( ); all the template function knows is that it can attach an argument list to the object as if it were a pointer to a function:

//: C05:FuncObject.cpp

// Simple function objects #include <iostream>

using namespace std;

263

template<class UnaryFunc, class T> void callFunc(T& x, UnaryFunc f) {

f(x);

}

void g(int& x) { x = 47;

}

struct UFunc {

void operator()(int& x) { x = 48;

}

};

int main() { int y = 0;

callFunc(y, g); cout << y << endl; y = 0;

callFunc(y, UFunc()); cout << y << endl;

} ///:~

The template callFunc( ) says “give me an f and an x, and I’ll write the code f(x).” In main( ), you can see that it doesn’t matter if f is a pointer to a function (as in the case of g( )), or if it’s a function object (which is created as a temporary object by the expression UFunc( )). Notice you can only accomplish this genericity with a template function; a non-template function is too particular about its argument types to allow such a thing. The STL algorithms use this flexibility to take either a function pointer or a function object, but you’ll usually find that creating a function object is more powerful and flexible.

The function object is actually a variation on the theme of a callback, which is described in the design patterns chapter. A callback allows you to vary the behavior of a function or object by passing, as an argument, a way to execute some other piece of code. Here, we are handing callFunc( ) a pointer to a function or a function object.

The following descriptions of function objects should not only make that topic clear, but also give you an introduction to the way the STL algorithms work.

Classification of function objects

Just as the STL classifies iterators (based on their capabilities), it also classifies function objects based on the number of arguments that their operator( ) takes and the kind of value returned by that operator (of course, this is also true for function pointers when you treat them

Chapter 15: Multiple Inheritance

264

Соседние файлы в предмете Программирование