// Praktikum 4.1 using an associative container (std::map) // Author: Hans-Peter Weber // Date: 20.02.04 #pragma warning( disable: 4786 ) #include using std::cout; using std::cerr; using std::endl; using std::ios; #include using std::string; #include using std::ifstream; using std::ofstream; #include int main() { typedef std::map< string, int > stringMap; stringMap words; string s; int numberOfWords = 0; ifstream in( "Max.txt" ); if( !in ) { cerr << "Datei konnte nicht geoeffnet werden." << endl; exit( 1 ); } // fill map 'words' with all words contained in file (implicitly sorted) and their frequencies while( in >> s ) { ++words[ s ]; numberOfWords++; } in.close(); // store words and frequencies in text file ofstream out( "WordList.txt", ios::app ); if( !out ) { cerr << "Datei konnte nicht geoeffnet werden." << endl; exit( 1 ); } string mapElement; for( stringMap::const_iterator wordIterator = words.begin(); wordIterator != words.end(); ++wordIterator ) { mapElement = wordIterator->first; mapElement.resize( 20, '.' ); // cout << mapElement << wordIterator->second << endl; out << mapElement << wordIterator->second << endl; } out.close(); cout << "Woerter in der Datei: " << numberOfWords << endl << "unterschiedliche Woerter: " << words.size() << endl << endl; return 0; }