LineIndex* make_index(istream& input, vector<string> const& entries) { LineIndex* index = new LineIndex; for(EntryIt p = entries.begin(); p != entries.end(); ++p) (*index)[*p]; // causes insertion ? string line, word; int line_number = 0; // read a line from input at a time into a string while(getline(input, line)){ ++line_number; // make an istream from the string istrstream words(static_cast<const char*>(line.c_str())); // read a word at a time from string while(words >> word) { // use find() on map<string, vector<int>> to see // if it contains the word already, if it does, // save the line number in the map's vector LineIndex::iterator p = index->find(word); if(p != index->end()) (*p).second.push_back(line_number); } } }