31 lines
568 B
C++
31 lines
568 B
C++
#include <fstream>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <map>
|
|
|
|
int main () {
|
|
const char fname[] = "../ex5.3/example.txt";
|
|
|
|
std::ifstream fh(fname);
|
|
|
|
if ( !fh ) {
|
|
std::cout << "Error opening (hardcoded) File '" << fname << "'" << std::endl;
|
|
return 2;
|
|
}
|
|
|
|
|
|
std::map<std::string,int> myMap;
|
|
std::string word;
|
|
|
|
while ( fh >> word ) {
|
|
myMap[word] += 1; // Lookup the 'word' key and increase its counter;
|
|
}
|
|
|
|
auto iter = myMap.begin();
|
|
while ( iter != myMap.end() ) {
|
|
std::cout << iter->first << ", " << iter->second << std::endl;
|
|
|
|
iter++;
|
|
}
|
|
|
|
}
|