#include #include #include #include #include "split.h" using std::cin; using std::cout; using std::endl; using std::getline; using std::istream; using std::string; using std::vector; using std::map; // find all the lines that refer to each word in the input map > xref(istream& in, vector find_words(const string&) = split) { string line; int line_number = 0; map > ret; // read the next line while (getline(in, line)) { ++line_number; // break the input line into words vector words = find_words(line); // remember that each word occurs on the current line #ifdef _MSC_VER for (std::vector::const_iterator it = words.begin(); #else for (vector::const_iterator it = words.begin(); #endif it != words.end(); ++it) ret[*it].push_back(line_number); } return ret; } int main() { // call `xref' using `split' by default map > ret = xref(cin); // write the results #ifdef _MSC_VER for (std::map >::const_iterator it = ret.begin(); #else for (map >::const_iterator it = ret.begin(); #endif it != ret.end(); ++it) { // write the word cout << it->first << " occurs on line(s): "; // followed by one or more line numbers #ifdef _MSC_VER std::vector::const_iterator line_it = it->second.begin(); #else vector::const_iterator line_it = it->second.begin(); #endif cout << *line_it; // write the first line number ++line_it; // write the rest of the lines numbers, if any while (line_it != it->second.end()) { cout << ", " << *line_it; ++line_it; } // write a new line to separate each word from the next cout << endl; } return 0; }