Mapping Words to Line Numbers in Text Files in STL / C++
Following on from the previous post , this example shows an example of how to use an STL multimap to track the line number(s) associated with each word in a text file. This program essentially reads in text line-by-line, while stripping out all occurrences of punctuation and other non-alphanumeric charcters. Each pair is inserted into the multimap container using the insert function. As with the previous posting, which deals with counting the frequency of words in a file, this example also uses the sample Hamlet.txt file . Code listing as follows: [code language="cpp"] #include <iostream> #include <sstream> #include <fstream> #include <map> using namespace std; int main() { const string path = "/home/andy/NetBeansProjects/Hamlet.txt"; //Linux //const string path = "C:\\Dump\\Hamlet.txt"; ifstream input( path.c_str() ); if ( !input ) { cout << "Error opening file." << endl; ...