Lesson of 14 July 2008¶
Use of map and vector of STL
Read a file and count the words
File wordcount/main.cc
:open:
1/*
2 A simple program that count words from a file
3 and print with some statistics.
4*/
5
6#include <iostream>
7#include <fstream>
8#include <cmath>
9
10// from STL
11#include <string>
12#include <map>
13
14/*
15 > main pippo pluto
16
17 argc = 3
18 argv[0] = "main"
19 argv[1] = "pippo"
20 argv[2] = "pluto"
21
22 Usage of the program
23
24 > main filename
25
26 */
27
28using namespace std ;
29
30int
31main( int argc, char * argv[] ) {
32 string fileName ;
33
34 if ( argc > 2 ) {
35 // too nuch argument, issue an error
36 cerr << "usage: " << argv[0] << " file\n" ;
37 return 1 ;
38 }
39
40 if ( argc == 1 ) {
41 // only one argument, ask to the user the file name
42 cout << "File: " ;
43 cin >> fileName ;
44 } else {
45 // two argument, get file name from command line
46 fileName = argv[1] ;
47 }
48
49 // open the file
50 ifstream file( fileName . c_str() ) ;
51
52 // check if the file exist is opened etc.
53 if ( file . bad() ) {
54 cerr << "error in openeing : " << fileName << "\n" ;
55 return 1 ;
56 }
57
58 map<string,int> wc ; // associative array to store word count
59
60 // loop to read all the words
61 while ( file . good() ) {
62 string w ;
63 file >> w ;
64
65 // check if w is present in the associative array wc
66 map<string,int>::iterator i = wc.find(w) ;
67 if ( i == wc.end() ) {
68 // element not found
69 wc[w] = 1 ;
70 } else {
71 // element found!
72 // wc[w]++ ;
73 // i->first keyword
74 // i->second value
75 i->second++ ;
76 }
77 }
78
79 // close the file
80 file . close() ;
81
82 // write the output
83 for ( map<string,int>::const_iterator i = wc.begin() ; i != wc.end() ; ++i ) {
84 cout << i -> first << " " << i -> second << "\n" ;
85 }
86}
Read a file and count the words, resort the count and print only the most frequents
:open:
1/*
2 A simple program that count word from a file
3 and print with some statistics.
4*/
5
6#include <iostream>
7#include <iomanip>
8#include <fstream>
9#include <cmath>
10#include <cctype>
11
12// from STL
13#include <string>
14#include <vector>
15#include <map>
16
17/*
18 > main pippo pluto
19
20 argc = 3
21 argv[0] = "main"
22 argv[1] = "pippo"
23 argv[2] = "pluto"
24
25 Usage of the program
26
27 > main filename
28
29 */
30
31
32using namespace std ;
33
34
35// comparator for sort (ascending order)
36typedef struct less_than {
37 //! return always true
38 bool operator () (pair<string,int> pa, pair<string,int> pb) const {
39 if ( pa . second < pb . second ) return true ;
40 if ( pa . second == pb . second ) {
41 return pa . first < pb . first ;
42 }
43 return false ;
44 }
45} ;
46
47// comparator for sort (sescending order)
48typedef struct greater_than {
49 //! return always true
50 bool operator () (pair<string,int> pa, pair<string,int> pb) const {
51 if ( pa . second > pb . second ) return true ;
52 if ( pa . second == pb . second ) {
53 return pa . first > pb . first ;
54 }
55 return false ;
56 }
57} ;
58
59less_than lt ;
60greater_than gt ;
61
62int
63main( int argc, char * argv[] ) {
64 string fileName ;
65
66 if ( argc > 2 ) {
67 // too much argument, issue an error
68 cerr << "usage: " << argv[0] << " file\n" ;
69 return 1 ;
70 }
71
72 if ( argc == 1 ) {
73 // only one argument, ask to the user the file name
74 cout << "File: " ;
75 cin >> fileName ;
76 } else {
77 // two argument, get file name from command line
78 fileName = argv[1] ;
79 }
80
81 // open the file
82 ifstream file( fileName . c_str() ) ;
83
84 // check if the file exist is opened etc.
85 if ( file . bad() ) {
86 cerr << "error in openeing : " << fileName << "\n" ;
87 return 1 ;
88 }
89
90 map<string,int> wc ; // word count
91
92 // loop to read all the words
93 while ( file . good() ) {
94 string w1, w ;
95 file >> w1 ;
96 w = "" ;
97 for ( string::iterator i = w1.begin() ; i != w1.end() ; ++i ) {
98 // string::value_type is the type of the element of the string
99 // normally string::value_type == char
100 string::value_type c = tolower(*i) ;
101 // consider only letters
102 if ( isalpha(c) ) w . push_back(c) ;
103 }
104
105 // check if w is present in the associative array wc
106 map<string,int>::iterator i = wc.find(w) ;
107 if ( i == wc.end() ) {
108 // element not found, create and initailize to 1
109 // only if lenght > 0
110 if ( w . length() > 0 ) wc[w] = 1 ;
111 } else {
112 // element found!
113 i->second++ ;
114 }
115
116 }
117
118 // close the file
119 file . close() ;
120
121 // define a vector to store the results
122 vector<pair<string,int> > vec ;
123 copy( wc.begin(), wc.end(), back_inserter(vec) ) ;
124
125 // the code is equivalent to
126 // for ( map<string,int>::const_iterator i = wc.begin() ;
127 // i != wc . end() ; ++i )
128 // vec . push_back(*i) ;
129 //
130 // the "back_inserter" function return an object which implement
131 // the operator ++ and =. ++ do nothing while
132 // back_inserter(vec) = rhs
133 // is transformed to vec.push_back(rhs)!.
134
135 // sort the vector
136 sort( vec.begin(), vec.end(), gt );
137
138 // write the output
139 int nmax = 10 ;
140 for ( vector<pair<string,int> >::const_iterator i = vec.begin() ;
141 i != vec.end() && nmax-- > 0 ; ++i ) {
142 cout << "word = " << setw(10) << left << i -> first
143 << " counted N. " << i -> second << " times\n" ;
144 }
145}
The lesson in a zip file
The zip file lesson6.zip