Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C+中创建映射到映射的倒排索引+;?_C++_Map_Indexing - Fatal编程技术网

C++ 如何在C+中创建映射到映射的倒排索引+;?

C++ 如何在C+中创建映射到映射的倒排索引+;?,c++,map,indexing,C++,Map,Indexing,我正在尝试从地图创建地图中的反向索引。目前我有以下代码: 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>

我正在尝试从地图创建地图中的反向索引。目前我有以下代码:

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';
        }
    }
}
intmain()
{
字符行缓冲区[200];
typedef std::map MapType;
标准::ifstream archiveInputStream(“/hola”);
//将单词映射到其文本频率
映射字数;
//阅读整个档案。。。
而(!archiveInputStream.eof())
{
//…一行一行
getline(lineBuffer,sizeof(lineBuffer));
char*currentToken=strtok(lineBuffer,“”);
//如果有代币。。。
while(currentToken!=NULL)
{
//…检查wordcounts中是否已有要更新的元素。。。
迭代器iter=wordcounts.find(currentToken);
if(iter!=wordcounts.end())
{
//…然后更新字数
++字数[currentToken];
}
其他的
{
//…或以新的字数开始
wordcounts.insert(
std::pair(currentToken,1));
}
currentToken=strtok(NULL,“”;//继续下一个标记
}
//显示内容
对于(MapType::const_迭代器it=wordcounts.begin();it!=wordcounts.end();
++(it)
{

std::cout我认为可能会有帮助的是创建第二个映射,通过该索引为具有相同字数索引的
字符串的列表编制索引,如下所示(类似于a):

std::映射倒置;

因此,当您完成创建
wordcounts
-映射时,您必须像这样手动将每个
字符串
插入反向索引(注意,此代码未经测试!):

//倒排索引的字数
for(std::map::iterator it=wordcounts.begin();
it!=wordcounts.end();++it)
{
int-wordcountOfString=it->second;
std::string currentString=it->first;
标准::映射::迭代器=
查找(wordcountOfString);
if(invertedIt==inverted.end())
{
//插入新列表
std::list newList;
newList.push_back(当前字符串);
反向插入(
std::make_对(
wordcountOfString,newList));
}
其他的
{
//更新现有列表
std::list&existingList=invertedIt->second;
existingList.push_back(当前字符串);
}
}

请更具体地说明您实际需要什么帮助,否则很难猜测感谢您的帮助。我需要帮助使用此代码映射创建一个反向索引。然后我需要创建类似的输出,并使用相应的频率输出单词。您是否尝试创建一个按频率索引的
映射
,以便t你可以做
freqm[42]
来得到出现
42次的单词吗?倒排索引的意思是什么?完全是直接的。我正在寻找确切的答案。@Christian看看这个答案中的图片:
// 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);
    }

}