Reputation: 5273
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!
Upvotes: 3
Views: 8565
Reputation: 1632
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
Upvotes: 0
Reputation: 254751
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);
}
Upvotes: 0
Reputation: 22780
the_vec
is passed as reference to constant, so you need const_iterator
:
map<string,unsigned int>::const_iterator map_iter;
Upvotes: 7