C++.


count()


template < class InputIterator, class Type >

iterator_traits<InputIterator>::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 );

 

list<string,allocator> textlines;

 

typedef list<string,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_traits<InputIterator>::

* distance_type dis_type;

*

* dis_type elem_count;

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

* search_item );

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

int elem_count = 0;

list<string,allocator>::iterator

ibegin = textlines.begin(),

iend = textlines.end();

 

// count()

count( ibegin, iend, search_item, elem_count );

 

cout << "count(): " << search_item

<< " " << elem_count << " ()\n";

}