#include #include #include #include #include using namespace std; #include "ctimer.h" /** * read a file, store all words in a vector, * then count occurrences of unique words * using a map to time different map implementations */ int main(int argc, char * argv[]) { ifstream input; string filename,s; int count; if (argc > 1) { filename = argv[1]; } else { cout << "filename "; cin >> filename; } input.open(filename.c_str()); map m; vector words; count = 0; while (input >> s) { words.push_back(s); count++; } map::iterator it; CTimer timer; timer.Start(); for(unsigned int k=0; k < words.size(); k++) { s = words[k]; it = m.find(s); if (it != m.end()) { it->second++; } else { m.insert(pair(s,1)); } } timer.Stop(); it = m.begin(); while (it != m.end()) { cout << it->second << "\t" << it->first << endl; it++; } cout << timer.ElapsedTime() << " for " << count << endl; cout << "map size = " << m.size() << endl; return 0; }