seach through a range of Input Iterators. The difference is that while find searches for one particular value, find_first_of searches for any of several values. Specifically, find_first_of searches for the first occurrance in the range [first1, last1) of any of the elements in [first2, last2). (Note that this behavior is reminiscent of the function strpbrk from the standard C library.)

The two versions of find_first_of differ in how they compare elements for equality. The first uses operator==, and the second uses and arbitrary user-supplied function object comp. The first version returns the first iterator i in [first1, last1) such that, for some iterator j in [first2, last2), *i == *j. The second returns the first iterator i in [first1, last1) such that, for some iterator j in [first2, last2), comp(*i, *j) is true. As usual, both versions return last1 if no such iterator i exists.

Definition

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

Requirements on types

For the first version:

• InputIterator is a model of Input Iterator.

• ForwardIterator is a model of Forward Iterator.

• InputIterator 's value type is EqualityComparable , and can be compared for equality with ForwardIterator's value type.

For the second version:

• InputIterator is a model of Input Iterator.

• ForwardIterator is a model of Forward Iterator.

• BinaryPredicate is a model of Binary Predicate.

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

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

Preconditions

• [first1, last1) is a valid range.

• [first2, last2) is a valid range.

Complexity

At most (last1 – first1) * (last2 – first2) comparisons.

Example

Like strpbrk, one use for find_first_of is finding whitespace in a string; space, tab, and newline are all whitespace characters.

int main() {

 const char* WS = ' ';

 const int n_WS = strlen(WS);

 char* s1 = 'This sentence contains five words.';

 char* s2 = 'OneWord';

 char* end1 = find_first_of(s1, s1 + strlen(s1), WS, WS + n_WS);

 char* end2 = find_first_of(s2, s2 + strlen(s2), WS, WS + n_WS);

 printf('First word of s1: %.*s ', end1 – s1, s1);

 printf('First word of s2: %.*s ', end2 – s2, s2);

}

See also

find, find_if, search

count

Category: algorithms

Component type: function

Prototype

Count is an overloaded name: there are two count functions.

template <class InputIterator, class EqualityComparable>

iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, const EqualityComparable& value);

template <class InputIterator, class EqualityComparable, class Size>

void count(InputIterator first, InputIterator last, const EqualityComparable& value, Size& n);

Description

Count finds the number of elements in [first, last) that are equal to value. More precisely, the first version of count returns the number of iterators i in [first, last) such that *i == value. The second version of count adds to n the number of iterators i in [first, last) such that *i == value.

The second version of count 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.

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.

• EqualityComparable is a model of Equality Comparable.

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

• An object of InputIterator's value type can be compared for equality with an object of type EqualityComparable.

For the second version, which takes four arguments:

• InputIterator is a model of Input Iterator.

• EqualityComparable is a model of Equality Comparable.

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

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

• An object of InputIterator's value type can be compared for equality with an object of type EqualityComparable.

Preconditions
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату