#include // for id STDIN_FILENO #include // for objects cout, cerr #include // for class ifstream #include // for class string #include // for class list #include // for class map ostream& operator << (ostream& out, const list & lst) // pre: out is writeable // post: list is written to out { bool addSpace = false; list::const_iterator iter; for (iter = lst.begin(); iter != lst.end(); iter++) { if (addSpace) out << " "; out << *iter; addSpace = true; } return out; } ostream& operator << (ostream& out, const map > & m) // pre: out is writeable // post: map is written to out { map >::const_iterator iter; for (iter = m.begin(); iter != m.end(); iter++) { out << iter->first << ": " << iter->second << endl; } return out; } int main (int argc, char * argv[]) { // assume we will be reading from the console ifstream input(STDIN_FILENO); // if passed an argument, assume it is a filename and read from it instead if (argc > 1) { input.close(); input.open(argv[1]); if (input.fail()) { cerr << argv[0] << "ERROR: could not open file: " << argv[1] << endl; return -1; } } map > words; // read each character and echo what is typed string word = ""; string line = ""; int lineNum = 1; char c; while (input.get(c)) { if (c == '\n') { cout << line << endl; if (word != "") { // cout << lineNum << ": " << word << endl; words[word].push_back(lineNum); word = ""; } line = ""; lineNum++; } else if ((c == ' ') || (c == '\t')) { if (word != "") { // cout << lineNum << ": " << word << endl; words[word].push_back(lineNum); word = ""; } line += c; } else { word += c; line += c; } } // print the table cout << words << endl; return 0; }