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», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.
Интервал:
Закладка:
The library contains two definitions of iterator_traits : a fully generic one, and a specialization that is used whenever the template argument is a pointer type [1]. The fully generic version defines iterator_traits::value_type as a synonym for I::value_type , iterator_traits::difference_type as a synonym for I::difference_type , and so on. Since pointers don't have nested types, iterator_traits has a different definition.
The implementation of iterator_traits is actually simpler than this discussion.
template
struct iterator_traits {
typedef typename Iterator::iterator_category iterator_category;
typedef typename Iterator::value_type value_type;
typedef typename Iterator::difference_type difference_type;
typedef typename Iterator::pointer pointer;
typedef typename Iterator::reference reference;
};
template
struct iterator_traits {
typedef random_access_iterator_tag iterator_category;
typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;
};
If you are defining a new iterator type I , then you must ensure that iterator_traits is defined properly. There are two ways to do this. First, you can define your iterator so that it has nested types I::value_type , I::difference_type , and so on. Second, you can explicitly specialize iterator_traits for your type. The first way is almost always more convenient, however, especially since you can easily ensure that your iterator has the appropriate nested types just by inheriting from one of the base classes input_iterator , output_iterator , forward_iterator , bidirectional_iterator , or random_access_iterator .
Note that iterator_traits is new; it was added to the draft C++ standard relatively recently. Both the old iterator tags mechanism and the new iterator_traits mechanism are currently supported [1 , but the old iterator tag functions are no longer part of the standard C++ library and they will eventually be removed.
This generic function returns the last element in a non-empty range. Note that there is no way to define a function with this interface in terms of the old value_type function, because the function's return type must be declared to be the iterator's value type.
template
iterator_traits::value_type last_value(InputIterator first, InputIterator last) {
iterator_traits::value_type result = *first;
for (++first; first != last; ++first) result = *first;
return result;
}
(Note: this is an example of how to use iterator_traits ; it is not an example of good code. There are better ways of finding the last element in a range of bidirectional iterators, or even forward iterators.)
Defined in the standard header iterator, and in the nonstandard backward-compatibility header iterator.h.
Parameter | Description |
---|---|
Iterator |
The iterator type whose associated types are being accessed. |
Default Constructible, Assignable
• Iterator is a model of one of the iterator concepts. (Input Iterator, Output Iterator, Forward Iterator, Bidirectional Iterator, or Random Access Iterator.)
None.
None, except for nested types.
Member | Description |
---|---|
iterator_category |
One of the types input_iterator_tag , output_iterator_tag , forward_iterator_tag , bidirectional_iterator_tag , or random_access_iterator_tag . An iterator's category is the most specific iterator concept that it is a model of. |
value_type |
Iterator 's value type, as defined in the Trivial Iterator requirements. |
difference_type |
Iterator 's distance type, as defined in the Input Iterator requirements. |
pointer |
Iterator 's pointer type: a pointer to its value type. |
reference |
Iterator 's reference type: a reference to its value type. |
[1] The iterator_traits class relies on a C++ feature known as partial specialization . Many of today's compilers don't implement the complete standard; in particular, many compilers do not support partial specialization. If your compiler does not support partial specialization, then you will not be able to use iterator_traits , and you will have to continue using the old iterator tag functions iterator_category , distance_type , and value_type . This is one reason that those functions have not yet been removed.
The iterator overview, iterator tags, input_iterator_tag, output_iterator_tag, forward_iterator_tag, bidirectional_iterator_tag, random_access_iterator_tag, input_iterator, output_iterator, forward_iterator, bidirectional_iterator, random_access_iterator
iterator_category
Category: iterators
Component type: function
Iterator_category is overloaded; it is in fact six different functions.
inline output_iterator_tag iterator_category(const output_iterator&);
template inline input_iterator_tag
iterator_category(const input_iterator&);
template inline forward_iterator_tag
iterator_category(const forward_iterator&);
template inline bidirectional_iterator_tag
iterator_category(const bidirectional_iterator&);
template inline random_access_iterator_tag
iterator_category(const random_access_iterator&);
template inline random_access_iterator_tag iterator_category(const T*);
Iterator_category is an iterator tag function: it is used to determine the category to which an iterator belongs. Specifically, every iterator must belong to a type that is a model of the concept Output Iterator, Input Iterator, Forward Iterator, Bidirectional Iterator, or Random Access Iterator. [1] Iterator_category returns an object of class output_iterator_tag , input_iterator_tag , forward_iterator_tag , or random_access_iterator_tag , depending on which concept the type of iterator_category 's argument is a model of. [2] This information is useful in the case of an algorithm that has a sensible definition for more than one category of iterator, but whose definition is different depending on the category.
Читать дальшеИнтервал:
Закладка:
Похожие книги на «Standard Template Library Programmer's Guide»
Представляем Вашему вниманию похожие книги на «Standard Template Library Programmer's Guide» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.
Обсуждение, отзывы о книге «Standard Template Library Programmer's Guide» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.