C++.


/ - 2


/p>

}

ofstream

. WordCount :

#include <fstream>

#include "WordCount.h"

 

int main()

{

// word.out

ofstream oFile( "word.out" );

// ...

 

// WordCount

WordCount artist( "Renoir" );

artist.found( 7, 12 ); artist.found( 34, 18 );

 

// <<(ostream&, const WordCount&);

oFile << artist;

}

, ifstream, istream. :

#include <fstream>

#include <string>

 

int main()

{

cout << "filename: ";

string file_name;

 

cin >> file_name;

 

//

ifstream inFile( file_name.c_str() );

 

if ( !inFile ) {

cerr << " : "

<< file_name << " -- !\n";

return -1;

}

 

char ch;

while ( inFile.get( ch ))

cout.put( ch );

}

, , alice_emma, filter_string()

(. 20.2.1, ), , :

#include <fstream>

#include <iterator>

#include <vector>

#include <algorithm>

 

template <class InputIterator>

void filter_string( InputIterator first, InputIterator last,

string filt_elems = string("\",?."))

{

for ( ; first != last; first++ )

{

string::size_type pos = 0;

while (( pos = (*first).find_first_of( filt_elems, pos ))

!= string::npos )

(*first).erase( pos, 1 );

}

}

 

int main()

{

ifstream infile( "alice_emma" );

 

istream_iterator<string> ifile( infile );

istream_iterator<string> eos;

 

vector< string > text;

copy( ifile, eos, inserter( text, text.begin() ));

 

string filt_elems( "\",.?;:" );

filter_string( text.begin(), text.end(), filt_elems );

 

vector<string>::iterator iter;

 

sort( text.begin(), text.end() );

iter = unique( text.begin(), text.end() );

text.erase( iter, text.end() );

ofstream outfile( "alice_emma_sort" );

 

iter = text.begin();

for ( int line_cnt = 1; iter != text.end();

++iter, ++line_cnt )

{

outfile << *iter << " ";

if ( ! ( line_cnt % 8 ))

outfile << '\n';

}

outfile << endl;

<