Search code examples
c++stlmultimap

c++ multimap insert more than two values


I have found that it's possible to declare such std::multimap:

multimap < u_int32_t, u_int32_t,string> lines;

If it's possible to declare it then it should be possible to insert too

But I wonder how?

I have tried std::pair, but it seems I need something like std::triple.

I know it's possible to decrale some struct and hold into that struct a few values. But I would rather prefer to do it directly. Moreover because it's possible to declare it.

EDIT
I did serious mistake and it turned out I really understood multimap wrong.
Screams of people here and downvotes made me to reread documentation. Now I use it so:

struct container {
u_int32_t  size_in_blocks;
string name_of_file;
};
            //size_of_file
multimap <  u_int32_t, container> lines;
       // first value is used as a key for sorting
       // second value is just a storage

container d;// initialization
lines.insert ( std::pair<u_int32_t,container>( total_size_bytes, d) );

Thanks all!


Solution

  • This is wrong:

    multimap < u_int32_t, u_int32_t,string> lines;
    

    The template parameters for multimap are listed at en.cppreference.com:

    template<
        class Key,
        class T,
        class Compare = std::less<Key>,
        class Allocator = std::allocator<std::pair<const Key, T> >
    > class multimap;
    

    The first template parameter is the key, the second is the type stored, and the third is the comparator.

    You have specified std::string as the comparator. Clearly this won't do what you want, and I'm somewhat suprised this even compiles. basic_string does have an operator< -- that must be why it compiles.

    I think you are confused as to what multimap really is. multimap is not something that can be used to map between a key and one of mopre different kinds of values. multimap is the same as map in that it maps between a single key and a value, except the difference is that with multimap you can have more than just one value mapped to a single key.