Hello,
I would like to define a map container which includes another map and inter map contains a set so I define it as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
#include <string>
#include <map>
#include <set>
template < typename KEY, typename VALUE, typename Face > // return iterator to map
typename std::map< KEY, std::map<VALUE, Face> >::iterator
inserted_at( std::map< KEY, std::map<Face, std::set<VALUE>> >& map, const KEY& key, const VALUE& value, const Face& face )
{
typename std::map<KEY, std::map<Face, std::set<VALUE>> >::iterator map_iter = map.find(key) ;
if( map_iter == map.end() ) // key was not found, insert { key, empty set }
{
std::map<Face, std::set<VALUE>> empty_set ;
map_iter = map.insert( std::make_pair( key, empty_set ) ).first ;
}
std::map<Face, std::set<VALUE>> myMap;
myMap=map_iter->second;
typename std::map<Face, std::set<VALUE>>::iterator map_itern=myMap.find(face);
if (map_itern==myMap.end())
{
std::set<VALUE> empty_setn;
map_itern=myMap.insert(std::make_pair(face, empty_setn)).first;
}
map_itern->second.insert(value) ; // insert value into set
return map_iter ; // return iterator of map
}
int main()
{
typedef std::map< std::string, std::map<int,std::set<int>> > map_type ;
map_type my_map ;
const map_type::iterator map_iter = inserted_at( my_map, std::string("abcd"), 19, 1 ) ;
}
|
However, I have received the following error:
map2.cpp: In instantiation of 'typename std::map<KEY, std::map<VALUE, Face> >::iterator inserted_at(std::map<KEY, std::map<Face, std::set<VALUE> > >&, const KEY&, const VALUE&, const Face&) [with KEY = std::__cxx11::basic_string<char>; VALUE = int; Face = int; typename std::map<KEY, std::map<VALUE, Face> >::iterator = std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > > > >]':
map2.cpp:48:51: required from here
map2.cpp:28:11: error: could not convert 'map_iter' from '_Rb_tree_iterator<pair<[...],map<[...],std::set<int>,[...],allocator<pair<[...],std::set<int>>>>>>' to '_Rb_tree_iterator<pair<[...],map<[...],int,[...],allocator<pair<[...],int>>>>>'
return map_iter ; // return iterator of map
May I know where I am doing mistake
Thanks