Алгоритм count()

We use cookies. Read the Privacy and Cookie Policy

Алгоритм count()

template class InputIterator, class Type

iterator_traitsInputIterator::distance_type

count( InputIterator first,

InputIterator last, const Type& value );

count() сравнивает каждый элемент со значением value в диапазоне, ограниченном парой итераторов [first,last), с помощью оператора равенства. Алгоритм возвращает число элементов, равных value. (Отметим, что в имеющейся у нас реализации стандартной библиотеки поддерживается более ранняя спецификация count().)

#include algorithm

#include string

#include list

#include iterator

#include assert.h

#include iostream.h

#include fstream.h

/***********************************************************************

* прочитанный текст:

Alice Emma has long flowing red hair. Her Daddy says

when the wind blows through her hair, it looks almost alive,

like a fiery bird in flight. A beautiful fiery bird, he tells her,

magical but untamed. "Daddy, shush, there is no such thing,"

she tells him, at the same time wanting him to tell her more.

Shyly, she asks, "I mean, Daddy, is there?"

************************************************************************

* программа выводит:

* count(): fiery встречается 2 раз(а)

************************************************************************

*/

int main()

{

ifstream infile( "alice_emma" );

assert ( infile != 0 );

liststring,allocator textlines;

typedef liststring,allocator::difference_type diff_type;

istream_iterator string, diff_type instream( infile ),

eos;

copy( instream, eos, back_inserter( textlines ));

string search_item( "fiery" );

/********************************************************************* *

примечание: ниже показан интерфейс count(), принятый в

* стандартной библиотеке. В текущей реализации библиотеки

* от RogueWave поддерживается более ранняя версия, в которой

* типа distance_type еще не было, так что count()

* возвращала результат в последнем аргументе

*

* вот как должен выглядеть вызов:

*

* typedef iterator_traitsInputIterator::

*distance_type dis_type;

*

* dis_type elem_count;

* elem_count = count( textlines.begin(), textlines.end(),

* search_item );

***********************************************************************

int elem_count = 0;

liststring,allocator::iterator

ibegin = textlines.begin(),

iend = textlines.end();

// устаревшая форма count()

count( ibegin, iend, search_item, elem_count );

cout "count(): " search_item

" встречается " elem_count " раз(а) ";

}