I am trying to create a program that takes in data from a .txt or similar file and asks the user for a word to search for. The output should show the keyword in context with the 2 words that were originally in front of it, as well as behind it. (EX: keyword: boy would output "and the boy ran away") I am able to find all instances of the keyword in the file with the equal_range() function, however I do not know how to iterate through the data in the map to access the other words for context. Here is my code so far:
typedef multimap<string, int> templateMap;
templateMap wordMap;
typedef pair<templateMap::iterator, templateMap::iterator> searchTemplate;
searchTemplate search;
typedef pair<templateMap::const_iterator, templateMap::const_iterator> innerIteratorTemplate;
multimap<string, int>::iterator tempMap;
string tempWord;
string keyword;
// omitted code
for (size_t i = 0; !inData.eof(); i++)
{
inData >> tempWord;
wordMap.insert(pair<string, int>(tempWord, i));
}
search = wordMap.equal_range(keyword);
for (multimap<string, int>::iterator itr = search.first; itr != search.second; ++itr)
{
cout << "The keyword " << keyword << " is found at location " << itr->second << endl;
tempMap = itr;
itr->second = itr->second - 2;
cout << itr->first << endl;
}
I am aware that the code in the for loop at the bottom is wrong, but it was for testing purposes.