is there an iterator across unique keys in a std::multimap?

前端 未结 6 1959
无人及你
无人及你 2020-11-29 08:18

Is there a simple or standard way to have a multimap iterator which iterate across unique keys in a multimap?

i.e. for a set that looks like: {1, \"a\"}, {1, \

6条回答
  •  醉话见心
    2020-11-29 08:44

    if you have to pass over all unique keys quickly then you can use std::map instead;

    typedef std::map< KeyType, std::list< ValueType > > MapKeyToMultiValue;
    

    Insertion would be more difficult, However you can iterate over all keys without having to bother with duplicate entries. Insertion would look as follows:

    void insert_m(MapKeyToMultiValue &map, const KeyType key, const ValueType value )
    {
      auto it = map.find( key );
      if (it == map.end())
      {
         std::list empty;
         std::pair< MapKeyToMultiValue::iterator, bool > ret =
            map.insert( MapKeyToMultiValue::value_type( key, empty ) );
         it = ret.first;
      }
    
      it->second.push_back( value );
    }
    

    or you can make that very templated:

    template > >
    void insert_multi( MapType &map, const KeyType key, const ValueType value )
    {
    
      auto it = map.find( key );
      if (it == map.end())
      {
         std::list empty;
         std::pair< typename MapType::iterator, bool > ret =
            map.insert( typename MapType::value_type( key, empty ) );
         it = ret.first;
      }
    
      it->second.push_back( value );
    }
    

    The full test program looks as follows:

    #include 
    #include 
    #include 
    #include 
    
    typedef std::string KeyType;  
    typedef int ValueType;
    
    typedef std::map< KeyType, std::list< ValueType > >  MapKeyToMultiValue;
    
    void insert_m(MapKeyToMultiValue &map, const KeyType key, const ValueType value )
    {
      auto it = map.find( key );
      if (it == map.end())
      {
         std::list empty;
         std::pair< MapKeyToMultiValue::iterator, bool > ret =
            map.insert( MapKeyToMultiValue::value_type( key, empty ) );
         it = ret.first;
      }
    
      it->second.push_back( value );
    }
    
    
    template > >
    void insert_multi( MapType &map, const KeyType key, const ValueType value )
    {
    
      auto it = map.find( key );
      if (it == map.end())
      {
         std::list empty;
         std::pair< typename MapType::iterator, bool > ret =
            map.insert( typename MapType::value_type( key, empty ) );
         it = ret.first;
      }
    
      it->second.push_back( value );
    }
    
    
    int main()
    {
        MapKeyToMultiValue map;
    
    
        insert_m(map, std::string("aaa"), 1 );
        insert_m(map, std::string("aaa"), 2 );
        insert_m(map, std::string("bb"), 3 );
        insert_m(map, std::string("cc"), 4 );
    
    
        insert_multi(map, std::string("ffffd"), 1 );
        insert_multi(map, std::string("ffffd"), 2 );
        insert_multi(map, std::string("ee"), 3 );
        insert_multi(map, std::string("ff"), 4 );
    
    
        for(auto i = map.begin(); i != map.end(); ++i)
        {
          printf("%s\n", i->first.c_str() );
        }
    
    
        return 0;
    }
    

提交回复
热议问题