summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog2/map/map.cpp
blob: c140a8814237830366c00188c6486dcadb13bd26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Praktikum 4.1 using an associative container (std::map)

// Author: Hans-Peter Weber

// Date: 20.02.04



#pragma warning( disable: 4786 )



#include <iostream>

using std::cout;

using std::cerr;

using std::endl;

using std::ios;



#include <string>

using std::string;



#include <fstream>

using std::ifstream;

using std::ofstream;



#include <map>



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;

}