如何在C++中创建从地图到映射的倒排索引
How to create an inverted index from a map to map in C++?
我正在尝试从地图在地图中创建倒排索引。目前我有这个代码:
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';
}
}
}
关于这个麻烦我不知道,因为我是使用地图结构的初学者。
非常感谢您的帮助。
我认为可能会有所帮助的是创建第二个地图,通过该索引索引索引具有相同字数索引的string
列表,如下所示(类似于直方图):
std::map<int, std::list<std::string> > inverted;
因此,当您完成创建 wordcounts
-map 时,您必须像这样手动将每个string
插入倒排索引中(请注意,此代码未经测试!
// wordcounts to inverted index
for (std::map<std::string, int>::iterator it = wordcounts.begin();
it != wordcounts.end(); ++it)
{
int wordcountOfString = it->second;
std::string currentString = it->first;
std::map<int, std::list<std::string> >::iterator invertedIt =
inverted.find(wordcountOfString);
if (invertedIt == inverted.end())
{
// insert new list
std::list<std::string> newList;
newList.push_back(currentString);
inverted.insert(
std::make_pair<int, std::list<std::string>>(
wordcountOfString, newList));
}
else
{
// update existing list
std::list<std::string>& existingList = invertedIt->second;
existingList.push_back(currentString);
}
}
相关文章:
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 为什么不;名字在地图上是按顺序排列的吗
- 将函数类成员映射到类本身内部
- 如何在 C# 中映射双 C 结构指针?
- 如何在C++中使用结构生成映射
- 使用std::函数映射对象方法
- 映射作为地图中的键
- EIGEN地图类:将C阵列映射到VectorXD指针以有效的方式
- 如何通过键和值以及在C 中的结果映射中比较两个地图?我们有任何STL API吗?
- 向量映射作为实例成员的C 地图
- 将映射从第二个元素复制到另一个地图
- 复制地图<双精度,元组<双精度,双精度>>映射<双精度,双精度>没有循环?
- 如何使用合并合并两个地图/多映射(C 11 STL)
- 如何将std ::映射的第一个N元素复制到另一个地图
- 如何在映射向量中循环访问地图
- 获取按地图值排序的映射键向量的最快方法
- 如何在将多维 int 数组映射到 C++ 中的字符串时实现在地图上查找或计数
- 如何在C++中创建从地图到映射的倒排索引
- 在多映射中,当两个迭代器持有具有映射到不同Value的相同键的值时.我们如何才能在地图上找到它们中的哪一个在另一个之前