/* * File: lecture.cpp * ------------------ * Snippets from the live coding part of Wed 1/23 lecture * showing use of CS106 class library. */ #include "genlib.h" #include #include #include "map.h" #include "random.h" #include "set.h" void ReadFile(ifstream &in, Map &m) { while (true) { string word; in >> word; if (in.fail()) break; if (m.containsKey(word)) m[word]++; else m[word] = 1; } Map::Iterator itr = m.iterator(); string max; int maxCount = 0; while (itr.hasNext()) { string key = itr.next(); if (m[key] > maxCount) { max = key; maxCount = m[key]; } } cout << "Max is " << max << " = " << maxCount << endl; } void TestRandom() { Set seen; while (true) { int num = RandomInteger(1, 100); if (seen.contains(num)) break; seen.add(num); } Set::Iterator itr = seen.iterator(); while (itr.hasNext()) cout << itr.next() << endl; } int main() { ifstream in("handout.txt"); Map counts; ReadFile(in, counts); Randomize(); TestRandom(); return 0; }