using nested map

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

Last edited on
The return type is:

typename std::map< KEY, std::map<VALUE, Face> >::iterator

What you want is

std::map< std::string, std::map<int,std::set<int>> >

So you forgot the set for your return type.

By the way: myMap is a local variable (line 19 just copies the data). Line 26 will not have any effect outside of the function.
Thank you so much for your points and help and I do not have error. I edited the code 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
38
39
40
41
42
#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<Face, std::set<VALUE>> >::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 ;
    }


typename std::map<Face, std::set<VALUE>>::iterator map_itern=map_iter->second.find(face);
if (map_itern==map_iter->second.end())
{
std::set<VALUE> empty_setn;
map_itern=map_iter->second.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 ) ;

    std::cout << map_iter->first << ' ' ; // abcd (key)

    
}


However, I am wondering how I can print the element of the inner map as well.

I will appreciate your help

Thanks
You can iterate over the map like any other container. I recommend range based for loops:
1
2
3
4
5
6
7
8
for(const std::map<int, std::set<int>>::value_type& map_val : map_iter->second)
{
    std::cout << map_val.first << ' ' ; // 1
    for(const int val : map_val.second)
    {
        std::cout << val << ' ' ; // 19
    }
}    
Thank you so much. it works
Topic archived. No new replies allowed.