简体   繁体   中英

How to iterate through a map in a vector of maps?

Code written in C++

Environment: Microsoft Visual Studio

I have a vector of maps. First I want to iterate through the first map, get its 'first' and 'second' and save them in some other structure I built (a map of vectors). And then I'll iterate through the left maps in my "vector of maps" and do the same...

Here's my vector of maps:

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

And here's the function that should do the work:

void Coogle::make_index(const myvec& the_vec)
{
    //SCAN THE FIRST MAP
    map<string,unsigned int>::iterator map_iter;
    index::iterator idx_iter = the_index.begin();
    for(map_iter=the_vec[0].begin(); map_iter!=the_vec[0].end(); ++map_iter)
    {

    }
}

The 'for' loop should iterate through the first map in the vector. I declared a map iterator because I need it to iterate though the map! Right? Why isn't it working?

the error:

IntelliSense: no operator "=" matches these operands

Thanks a lot !!!


OK now I identified this iterator:

index::iterator idx_iter = the_index.begin();

And here's my 'index':

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

And inside the mentioned 'for' loop I did the following:

    for(map_iter=the_vec[0].begin(); map_iter!=the_vec[0].end(); ++map_iter)
    {
        /*#1*/ idx_iter->first = map_iter->first;
        /*#2*/ idx_iter->second[0] = map_iter->second;
        /*#3*/ idx_iter++;
    }

#2 seems to be ok. But #1 generates an error:

IntelliSense: no operator "=" matches these operands

It's the same error like the one before so I'm guessing it's a similar problem. Is it?

edit: To be more clear, what I wanna do is add from const myvec& the_vec to the 'i' location (in this case '0') to my index.

Again :

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

Thank you!

the_vec作为常量的引用传递,因此您需要const_iterator

map<string,unsigned int>::const_iterator map_iter;

To answer your second question, you can't modify the keys in a map, since they are required to be kept in order. If you want to replace all the elements in the index, then it's probably best to clear it first, then add the new elements:

the_index.clear();
for(map_iter=the_vec[0].begin(); map_iter!=the_vec[0].end(); ++map_iter)
{
    the_index[map_iter->first].push_back(map_iter->second);
}

Here is a way to iterate through a map that contains keys of string s and values of vector s of string s:

for( map<string, vector<string>>::iterator ii = timeZonesMap.begin(); ii != timeZonesMap.end(); ++ii)
{
    cout << ii->first << ": " << endl;
    for(std::vector<string>::iterator it = ii->second.begin(); it != ii->second.end(); ++it) {
        std::cout << *it << endl;
    }
}

Hopefully this helps someone in the future

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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