Standard Template Library Programmer's Guide

Здесь есть возможность читать онлайн «Standard Template Library Programmer's Guide» весь текст электронной книги совершенно бесплатно (целиком полную версию без сокращений). В некоторых случаях можно слушать аудио, скачать через торрент в формате fb2 и присутствует краткое содержание. Жанр: Программирование, Справочники, на английском языке. Описание произведения, (предисловие) а так же отзывы посетителей доступны на портале библиотеки ЛибКат.

Standard Template Library Programmer's Guide: краткое содержание, описание и аннотация

Предлагаем к чтению аннотацию, описание, краткое содержание или предисловие (зависит от того, что написал сам автор книги «Standard Template Library Programmer's Guide»). Если вы не нашли необходимую информацию о книге — напишите в комментариях, мы постараемся отыскать её.

This document contains reference on SGI STL implementation

Standard Template Library Programmer's Guide — читать онлайн бесплатно полную книгу (весь текст) целиком

Ниже представлен текст книги, разбитый по страницам. Система сохранения места последней прочитанной страницы, позволяет с удобством читать онлайн бесплатно книгу «Standard Template Library Programmer's Guide», без необходимости каждый раз заново искать на чём Вы остановились. Поставьте закладку, и сможете в любой момент перейти на страницу, на которой закончили чтение.

Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать
See also

count_if , find , find_if

count_if

Category: algorithms

Component type: function

Prototype

Count_if is an overloaded name: there are two count_if functions.

template iterator_traits::difference_type count_if(InputIterator first, InputIterator last, Predicate pred);

template

void count_if(InputIterator first, InputIterator last, Predicate pred, Size& n);

Description

Count_if finds the number of elements in [first, last) that satisfy the predicate pred . More precisely, the first version of count_if returns the number of iterators i in [first, last) such that pred(*i) is true . The second version of count adds to n the number of iterators i in [first, last) such that pred(*i) is true .

The second version of count_if was the one defined in the original STL, and the first version is the one defined in the draft C++ standard; the definition was changed because the older interface was clumsy and error-prone. The older interface required the use of a temporary variable, which had to be initialized to 0 before the call to count_if .

Both interfaces are currently supported [1], for reasons of backward compatibility, but eventually the older version will be removed.

Definition

Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.

Requirements on types

For the first version, which takes three arguments:

• InputIterator is a model of Input Iterator.

• Predicate is a model of Predicate.

• InputIterator 's value type is convertible to Predicate 's argument type.

For the second version, which takes four arguments:

• InputIterator is a model of Input Iterator.

• Predicate is a model of Predicate.

• Size is an integral type that can hold values of InputIterator 's distance type.

• InputIterator 's value type is convertible to Predicate 's argument type.

Preconditions

For the first version:

• [first, last) is a valid range.

For the second version:

• [first, last) is a valid range.

• n plus the number of elements that satisfy pred does not exceed the maximum value of type Size .

Complexity

Linear. Exactly last – first applications of pred .

Example

int main() {

int A[] = { 2, 0, 4, 6, 0, 3, 1, –7 };

const int N = sizeof(A) / sizeof(int);

cout << "Number of even elements: " << count_if(A, A + N, compose1(bind2nd(equal_to(), 0), bind2nd(modulus(), 2))) << endl;

}

Notes

[1] The new count interface uses the iterator_traits class, which 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 the newer version of count , or any other STL components that involve iterator_traits .

See also

count , find , find_if

mismatch

Category: algorithms

Component type: function

Prototype

Mismatch is an overloaded name; there are actually two mismatch functions.

template

pair mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);

template

pair mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate binary_pred);

Description

Mismatch finds the first position where the two ranges [first1, last1) and [first2, first2 + (last1 – first1)) differ. The two versions of mismatch use different tests for whether elements differ.

The first version of mismatch finds the first iterator i in [first1, last1) such that *i != *(first2 + (i – first1)) . The return value is a pair whose first element is i and whose second element is *(first2 + (i – first1)) . If no such iterator i exists, the return value is a pair whose first element is last1 and whose second element is *(first2 + (last1 – first1)) .

The second version of mismatch finds the first iterator i in [first1, last1) such that binary_pred(*i, *(first2 + (i – first1)) is false . The return value is a pair whose first element is i and whose second element is *(first2 + (i – first1)) . If no such iterator i exists, the return value is a pair whose first element is last1 and whose second element is *(first2 + (last1 – first1)) .

Definition

Defined in the standard header algorithm, and in the nonstandard backward-compatibility header algo.h.

Requirements on types

For the first version:

• InputIterator1 is a model of Input Iterator.

• InputIterator2 is a model of Input Iterator.

• InputIterator1 's value type is a model of Equality Comparable.

• InputIterator2 's value type is a model of Equality Comparable.

• InputIterator1 's value type can be compared for equality with InputIterator2 's value type.

For the second version:

• InputIterator1 is a model of Input Iterator.

• InputIterator2 is a model of Input Iterator.

• BinaryPredicate is a model of Binary Predicate.

• InputIterator1 's value type is convertible to BinaryPredicate 's first argument type.

• InputIterator2 's value type is convertible to BinaryPredicate 's second argument type.

Preconditions

• [first1, last1) is a valid range.

• [first2, first2 + (last2 – last1)) is a valid range.

Complexity

Linear. At most last1 – first1 comparisons.

Example

int A1[] = { 3, 1, 4, 1, 5, 9, 3 };

int A2[] = { 3, 1, 4, 2, 8, 5, 7 };

const int N = sizeof(A1) / sizeof(int);

pair result = mismatch(A1, A1 + N, A2);

cout << "The first mismatch is in position " << result.first – A1 << endl;

cout << "Values are: " << *(result.first) << ", " << *(result.second) << endl;

Читать дальше
Тёмная тема
Сбросить

Интервал:

Закладка:

Сделать

Похожие книги на «Standard Template Library Programmer's Guide»

Представляем Вашему вниманию похожие книги на «Standard Template Library Programmer's Guide» списком для выбора. Мы отобрали схожую по названию и смыслу литературу в надежде предоставить читателям больше вариантов отыскать новые, интересные, ещё непрочитанные произведения.


Отзывы о книге «Standard Template Library Programmer's Guide»

Обсуждение, отзывы о книге «Standard Template Library Programmer's Guide» и просто собственные мнения читателей. Оставьте ваши комментарии, напишите, что Вы думаете о произведении, его смысле или главных героях. Укажите что конкретно понравилось, а что нет, и почему Вы так считаете.

x