Standard Template Library Programmer's Guide
Здесь есть возможность читать онлайн «Standard Template Library Programmer's Guide» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Жанр: Программирование, Справочники, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.
- Название:Standard Template Library Programmer's Guide
- Автор:
- Жанр:
- Год:неизвестен
- ISBN:нет данных
- Рейтинг книги:4 / 5. Голосов: 1
-
Избранное:Добавить в избранное
- Отзывы:
-
Ваша оценка:
- 80
- 1
- 2
- 3
- 4
- 5
Standard Template Library Programmer's Guide: краткое содержание, описание и аннотация
Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «Standard Template Library Programmer's Guide»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.
Standard Template Library Programmer's Guide — читать онлайн бесплатно полную книгу (весь текст) целиком
Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «Standard Template Library Programmer's Guide», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.
Интервал:
Закладка:
Mem_fun1_t 's constructor takes a pointer to one of X 's member functions. Then, like all function objects, mem_fun1_t has an operator() that allows the mem_fun1_t to be invoked with ordinary function call syntax. In this case, mem_fun1_t 's operator() takes two arguments; the first is of type X* and the second is of type Arg .
If F is a mem_fun1_t that was constructed to use the member function X::f , and if x is a pointer of type X* and a is a value of type Arg , then the expression F(x, a) is equivalent to the expression x->f(a) . The difference is simply that F can be passed to STL algorithms whose arguments must be function objects.
Mem_fun1_t is one of a family of member function adaptors. These adaptors are useful if you want to combine generic programming with inheritance and polymorphism, since, in C++, polymorphism involves calling member functions through pointers or references.
As with many other adaptors, it is usually inconvenient to use mem_fun1_t 's constructor directly. It is usually better to use the helper function mem_fun [2] instead.
struct Operation {
virtual double eval(double) = 0;
};
struct Square : public Operation {
double eval(double x) { return x * x; }
};
struct Negate : public Operation {
double eval(double x) { return –x; }
};
int main() {
vector operations;
vector operands;
operations.push_back(new Square);
operations.push_back(new Square);
operations.push_back(new Negate);
operations.push_back(new Negate);
operations.push_back(new Square);
operands.push_back(1);
operands.push_back(2);
operands.push_back(3);
operands.push_back(4);
operands.push_back(5);
transform(operations.begin(), operations.end(), operands.begin(), ostream_iterator(cout, "\n"), mem_fun(Operation::eval));
}
Defined in the standard header functional, and in the nonstandard backward-compatibility header function.h.
Parameter | Description |
---|---|
Result |
The member function's return type. |
X |
The class whose member function the mem_fun1_t invokes. |
Arg |
The member function's argument type. |
Adaptable Binary Function
• X has at least one member function that takes a single argument of type Arg and that returns a value of type Result . [1]
binary_function
Member | Where defined | Description |
---|---|---|
first_argument_type |
Adaptable Binary Function | The type of the first argument: X* |
second_argument_type |
Adaptable Binary Function | The type of the second argument: Arg |
result_type |
Adaptable Binary Function | The type of the result: Result |
Result operator()(X* x, Arg a) const |
Binary Function | Function call operator. Invokes x->f(a) , where f is the member function that was passed to the constructor. |
explicit mem_fun1_t(Result (X::*f)(Arg)) |
mem_fun1_t |
See below. |
template mem_fun1_t mem_fun(Result (X::*f)(Arg)); [2] |
mem_fun1_t |
See below. |
These members are not defined in the Adaptable Binary Function requirements, but are specific to mem_fun1_t .
Member | Description |
---|---|
explicit mem_fun1_t(Result (X::*f)(Arg)) |
The constructor. Creates a mem_fun1_t that calls the member function f . |
template mem_fun1_t mem_fun(Result (X::*f)(Arg)); [2] |
If f is of type Result (X::*)(Arg) then mem_fun(f) is the same as mem_fun1_t(f) , but is more convenient. This is a global function, not a member function. |
[1] The type Result is permitted to be void . That is, this adaptor may be used for functions that return no value. However, this presents implementation difficulties. According to the draft C++ standard, it is possible to return from a void function by writing return void instead of just return . At present, however (early 1998), very few compilers support that feature. As a substitute, then, mem_fun1_t uses partial specialization to support void member functions. If your compiler has not implemented partial specialization, then you will not be able to use mem_fun1_t with member functions whose return type is void .
[2] This helper function was called mem_fun1 in drafts of the C++ standard, but it is called mem_fun in the final standard. This implementation provides both versions for backward compatibility, but mem_fun1 will be removed in a future release.
mem_fun_t , mem_fun_ref_t , mem_fun1_ref_t
mem_fun1_ref_t
Categories: functors, adaptors
Component type: type
Mem_fun1_ref_t is an adaptor for member functions. If X is some class with a member function Result X::f(Arg) (that is, a member function that takes one argument of type Arg and that returns a value of type Result [1]), then a mem_fun1_ref_t is a function object adaptor that makes it possible to call f as if it were an ordinary function instead of a member function.
Mem_fun1_ref_t 's constructor takes a pointer to one of X 's member functions. Then, like all function objects, mem_fun1_ref_t has an operator() that allows the mem_fun1_ref_t to be invoked with ordinary function call syntax. In this case, mem_fun1_ref_t 's operator() takes two arguments; the first is of type X and the second is of type Arg .
If F is a mem_fun1_ref_t that was constructed to use the member function X::f , and if x is an object of type X and a is a value of type Arg , then the expression F(x, a) is equivalent to the expression x.f(a) . The difference is simply that F can be passed to STL algorithms whose arguments must be function objects.
Mem_fun1_ref_t is one of a family of member function adaptors. These adaptors are useful if you want to combine generic programming with inheritance and polymorphism, since, in C++, polymorphism involves calling member functions through pointers or references. In fact, though, mem_fun1_ref_t is usually not as useful as mem_fun1_t . The difference between the two is that mem_fun1_t 's first argument is a pointer to an object while mem_fun1_ref_t 's argument is a reference to an object. References, unlike pointers, can't be stored in STL containers: pointers are objects in their own right, but references are merely aliases.
Читать дальшеИнтервал:
Закладка:
Похожие книги на «Standard Template Library Programmer's Guide»
Представляем Вашему вниманию похожие книги на «Standard Template Library Programmer's Guide» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.
Обсуждение, отзывы о книге «Standard Template Library Programmer's Guide» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.