Thread: iterators

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >since you are not allowed to say map < string, int> mapobject?
    Why not?
    My best code is written with the delete key.

  3. #18
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >becuse you said that too me
    >"Because a pair is the value that maps contain."
    Yes.
    Code:
    map< string, pair<int, float> > k;
    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 be
    Code:
    k.insert(pair< int, pair<int, float> >(10, pair<int, float>(20, 123.4f)));
    If you care to try and understand that, of course. Or you can just take my word on it.
    Code:
    map<int, int> k;
    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:
    k.insert(pair<int, int>(10, 20));
    My best code is written with the delete key.

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  6. #21
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  7. #22
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  8. #23
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>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.

  9. #24
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of strings with iterators.
    By Mario F. in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2006, 12:12 PM
  2. Using reverse iterators in algorithms
    By 0rion in forum C++ Programming
    Replies: 1
    Last Post: 02-27-2006, 03:19 AM
  3. Writing Iterators...
    By Geolingo in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2003, 09:31 PM
  4. accessing vector elements: iterators are faster?
    By Captain Penguin in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2002, 02:27 PM
  5. Generic Progpammimg Iterators
    By rip1968 in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2002, 10:20 AM