>why does the insert function work only for "pair" objects?
Because a pair is the value that maps contain.
>why does the insert function work only for "pair" objects?
Because a pair is the value that maps contain.
My best code is written with the delete key.
>since you are not allowed to say map < string, int> mapobject?
Why not?
My best code is written with the delete key.
>becuse you said that too me
>"Because a pair is the value that maps contain."
Yes.
This is a map where the value of each element is a pair. The first part of the pair is a string, the second part is a pair of int and float. An insertion would beCode:map< string, pair<int, float> > k;
If you care to try and understand that, of course. Or you can just take my word on it.Code:k.insert(pair< int, pair<int, float> >(10, pair<int, float>(20, 123.4f)));
In this map, the first part of the pair is int and the second part of the pair is int. The insertion is much simpler:Code:map<int, int> k;
Code:k.insert(pair<int, int>(10, 20));
My best code is written with the delete key.
>this is the declaration:
>map< string, pair<int, float> > k;
I was thinking of a different declaration, sorry.
>are the elements kept in sorted order in the map?
It behaves as if they are, yes.
>that means fast access but slow insertion.
No, the standard gives time complexity guarantees. Insertion is logarithmic.
My best code is written with the delete key.
>ok, you mean it's like a sorted list?
I mean it's like a balanced binary search tree.
>and behaves like the <set> container?
Yes, the set container has similar guarantees.
My best code is written with the delete key.
>but a sorted list would take the same time for an insertion.
If only that were the case. The time complexity for a sorted list is linear. The sorted nature makes searches faster on average, but it's still a linear operation. Logarithmic is much faster.
My best code is written with the delete key.
>then what's the benefit of a liust over a binary tree?
They're considerably easier to work with, and sometimes you just don't need the punch of a tree.
My best code is written with the delete key.
>>are used only for sorted stuff, right?
And for mapping, which I suppose might fall under sorted. For example if you had a game, and you wanted to be able to dynamically bind keys to certain functions, you might have a key/function pointer map or something like that. So whan a key is pressed, you go "command[key]();" At least that's how I might do it.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.
I believe multimap and multiset would be used for when you don't want duplicate elements ignored. For example, if you try insert()'ing an object into a map but an identical one is already there, it doesn't insert the new object and just returns the existing one. In fact, I think that's how the [] operator works; it calls insert() and returns the .second of the return value.
Just Google It. √
(\ /)
( . .)
c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.