I’m trying to create an inverted index in a map from a map .At moment I have this code:
int main()
{
char lineBuffer[200];
typedef std::map<std::string, int> MapType;
std::ifstream archiveInputStream("./hola");
// map words to their text-frequency
std::map<std::string, int> wordcounts;
// read the whole archive...
while (!archiveInputStream.eof())
{
//... line by line
archiveInputStream.getline(lineBuffer, sizeof(lineBuffer));
char* currentToken = strtok(lineBuffer, " ");
// if there's a token...
while (currentToken != NULL)
{
// ... check if there's already an element in wordcounts to be updated ...
MapType::iterator iter = wordcounts.find(currentToken);
if (iter != wordcounts.end())
{
// ... then update wordcount
++wordcounts[currentToken];
}
else
{
// ... or begin with a new wordcount
wordcounts.insert(
std::pair<std::string, int>(currentToken, 1));
}
currentToken = strtok(NULL, " "); // continue with next token
}
// display the content
for (MapType::const_iterator it = wordcounts.begin(); it != wordcounts.end();
++it)
{
std::cout << "Who(key = first): " << it->first;
std::cout << " Score(value = second): " << it->second << '\n';
}
}
}
About this trouble I haven’t idea, because I’m beginner using map structure.
I’m very grateful to you your help.
I think what might help would be to create a second map, indexing lists of
stringwith same wordcount-index by this index, like this (similar to a histogram):std::map<int, std::list<std::string> > inverted;so when you’re done with creating the
wordcounts-map you have to insert everystringinto the inverted index manually like this (be careful, this code is untested!):