我正在尝试从 map 在 map 中创建倒排索引。此刻我有以下代码:
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';
}
}
}
关于这个麻烦我不知道,因为我是使用地图结构的初学者。
我非常感谢你的帮助。