简体   繁体   English

将地图数据的向量复制到向量地图中

[英]copying vector of maps data into a map of vectors

Sorry for posting the same problem again, but I used the suggested solutions, and it partially worked... So I want to clarify the problem: 抱歉再次发布相同的问题,但是我使用了建议的解决方案,并且部分起作用了...所以我想澄清一下问题:

So: 所以:

I defined a vector of maps, like this: 我定义了一个地图矢量,如下所示:

typedef vector<map<string,unsigned int> > myvec;

And a map of vectors (i called it index) like this: 以及矢量映射(我称之为索引),如下所示:

typedef map<string,vector<unsigned int> > index;

Then I did the following: 然后,我执行以下操作:

In my class, called myCoogle, i declared myvec maps_vec; 在名为myCoogle的课程中,我声明了myvec maps_vec;

and I filled it with maps... 我把地图填满了...

In each map there's a word (string) and a number (unsinged int). 在每个映射中都有一个单词(字符串)和一个数字(未整数)。

So far so good. 到现在为止还挺好。

I declared also index the_index; 我还声明了index the_index;

Now I want to copy all the different words from the_vec to the_index. 现在,我要将所有不同的单词从the_vec复制到the_index。

The words will be the strings... 单词将是字符串...

And for each vector i will be adding the numbers stored in the vector of maps. 对于每个向量,我将添加存储在地图向量中的数字。

For example: 例如:

the_vec has 3 maps. the_vec有3个地图。

the 1st has: chicken,1 | 第一个有:鸡肉1 | person,1 | 人,1 | elevator,5 | 电梯,5 | is,2 | 是2 | ... ...

the 2nd has: person,2 | 第二个有:person,2 | icecream,3 | 冰淇淋3 | is,3 | 是3 | ... ...

the 3rd has: elevator,1 | 第三有:电梯,1 | bear,1 | 熊1 | is,4 | 是4 | chicken,3 | 鸡3 | ... ...

So the_index should look like this: 所以the_index应该看起来像这样:

word,[vector of ints] 单词,[整数向量]

chicken[1,0,3] 鸡[1,0,3]

person,[1,2,0] 人,[1,2,0]

elevator[5,0,1] 电梯[5,0,1]

is[2,3,4] 是[2,3,4]

icecream[0,3,0] 冰淇淋[0,3,0]

bear[0,0,1] 熊[0,0,1]

OK here's my function: 好的,这是我的功能:

void Coogle::make_index()
{
    //SCAN THE FIRST MAP
    myvec::iterator myvec_iter;
    map<string,unsigned int>::iterator map_iter;
    index::iterator idx_iter = the_index.begin();
    for(map_iter=maps_vec[0].begin(); map_iter!=maps_vec[0].end(); ++map_iter)
    {
        the_index[map_iter->first].push_back(map_iter->second);
    }


    //SCAN THE OTHER MAPS
    myvec_iter=maps_vec.begin();
    myvec_iter++;
    int i=0; //FILE #
    while(myvec_iter!=maps_vec.end())
    {
        i++;
        for(map_iter=maps_vec[i].begin(); map_iter!=maps_vec[i].end(); ++map_iter)
        {
            string word=map_iter->first;
            cout << "DEALING WITH WORD \"" << word << "\"" << endl;
            index::iterator location;
            location=the_index.find(word);
            if(location!=the_index.end()) //if word found in the index
            {
                cout << "WORD EXISTS!" << endl;
                location->second[i]=map_iter->second;
            }
            else //if not found
            {
                cout << "WORD DOES NOT EXIST! NEW WORD." << endl;
                the_index[word].push_back(map_iter->second);
            }
        }
        cout << endl;
        ++myvec_iter;
    }
}

clarification: FILE# is the maps number... I'm working with files (*.txt files). 澄清:FILE#是地图编号...我正在处理文件(* .txt文件)。

Alright so after I scanned the first map, I tried to print the_index and all was fine. 好了,所以我扫描完第一张地图后,尝试打印the_index,一切都很好。 But I get this when trying to print after scanning also the other maps: 但是我在扫描其他地图后尝试打印时得到了这个:

错误

'Build Successfully' though. 但是,“成功构建”。

And this window pops up when I run the program. 当我运行程序时,该窗口弹出。

So I believe something is wrong with my 2nd 'for' loop. 因此,我相信我的第二个“ for”循环出了问题。

Anyone can help please? 有人可以帮忙吗?

Very sorry for the very long post... 非常长的帖子非常抱歉...

Thank you very much !!! 非常感谢你 !!!

edit: If I don't try to print the_index, the program compiles and runs just fine. 编辑:如果我不尝试打印the_index,该程序将编译并运行正常。 But that's not enough ofcourse. 但这当然还不够。 But my print function is just alright... here: 但是我的打印功能还可以...在这里:

void Coogle::print_index() const
{
    index::const_iterator iter;
    for(iter=the_index.begin();iter!=the_index.end();++iter)
    {
        cout << "Word: " << iter->first << endl;
        //cout << "Files number is: " << files_number << endl; //prints: 3
        for(int i=0; i<files_number;i++)
            cout << "file #" << i+1 << ": " << iter->second[i] << " , " << endl;
    }
}

edit: 编辑:

Here's a screen shot of printing only 1 map vs printing 3 maps: 以下是仅打印1张地图与打印3张地图的屏幕截图:

1图vs 3

Edit: Ok hopefully I finally understand what you're trying to do. 编辑:好的,希望我终于明白了您要做什么。

typdef myvec::iter vmiter_t;
typdef map<string,unsigned int>::iter miter_t;
typdef set<string>::iter siter_t;

set<string> words;

for(vmiter_t vmiter=maps_vec.begin(); vmiter != maps_vec.end(); ++vmiter)
{
    for(miter_t miter = vmiter->begin(); miter != vmiter->.end(); ++miter)
    {
        words.insert(miter->first);
    }
}

for (siter_t iter=words.begin(); iter!=words.end(); ++iter){
    const string& word = *iter;
    for(vmiter_t vmiter=maps_vec.begin(); vmiter != maps_vec.end(); ++vmiter)
    {
        map<string,unsigned int>& temp = *vmiter;
        the_index[word].push_back(temp[word]);
    }
}

It's hard to tell since you didn't provide the declaration of your variables (and some of them seem to be misnamed), but I think the problem is the line location->second[i]=map_iter->second; 很难说,因为您没有提供变量的声明(其中有些似乎被错误命名),但是我认为问题出在行location->second[i]=map_iter->second; . You are trying to access index i in the vector, but your vector isn't actually that long. 您正在尝试访问向量中的索引i,但是向量实际上没有那么长。

Edit: Now that I can tell what you're actually trying to do, you're massively overcomplicating things. 编辑:现在,我可以告诉您您实际上要做什么,您就使事情变得过于复杂。 You don't need a seperate loop for the other elements at all. 您根本不需要其他元素的单独循环。

Here's a C++11 answer: 这是C ++ 11的答案:

myvec v = /* ... */;

index q;

for (auto const & m : v)
    for (auto const & p : m)
        q[p->first].emplace_back(p->second);

It's the same as @Antimony's solution, just a bit easier on the eye. 它与@Antimony的解决方案相同,只是在眼睛上容易一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM