How to convert c++ std::list element to multimap iterator
Hello all
i have
std::list<multimap<std::string,std::string>::iterator> >
now i have new element :
multimap<std::string,std::string>::value_type aNewMmapValue("foo1","test")
i want to avoid the need to set temp multimap and do insert to the new element just to get its iterator back
so i could to push it back to the:
std::list<multimap<std::string,std::string>::iterator> >
can i somehow avoid this creation of the temp multimap.
Re: How to convert c++ std::list element to multimap iterator
If you are storing a list of multimap iterators, then the original values MUST exist in a multimap somewhere, otherwise the iterator to it will be invalid.
Also be aware that if the multimap should re-assign it's internal storage then all of your iterators in the list will be invalidated.
Re: How to convert c++ std::list element to multimap iterator
Quote:
Originally Posted by
umen
Hello all
i have
std::list<multimap<std::string,std::string>::iterator> >
now i have new element :
multimap<std::string,std::string>::value_type aNewMmapValue("foo1","test")
i want to avoid the need to set temp multimap and do insert to the new element just to get its iterator back
so i could to push it back to the:
std::list<multimap<std::string,std::string>::iterator> >
can i somehow avoid this creation of the temp multimap.
I'm trying to understand the reason behind you trying to convert an iterator in the first place.
If you recall, map::insert() that takes a single argument of value_type() returns a pair,
so you can do something like
Code:
#include <iostream>
#include <string>
#include <list>
#include <map>
using namespace std;
int main()
{
typedef multimap<string, string>::value_type valType;
typedef multimap<string, string>::iterator iterType;
// sample data
multimap<string, string> mMap;
list<iterType> List;
valType viter("Key", "Value");
List.push_back(mMap.insert(viter));
return 0;
}
without creating a temp map, so there's no need for conversion here.
But I could be totally wrong about what you're asking here.
If this was way off,
then maybe use std::pair? (I haven't tried it yet) to make a bridge?
or something like that...