Associative containers and iterators

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anonymous

    Associative containers and iterators

    I have a function that returns a map<string, vector<int.
    Inside the function I load the map with this instruction:

    loc_map["hello"].push_back(1);

    I would like to remove the output data structure and to use a template
    function with iterators.

    My idea is to use an iterator in this way:

    template <class Out>
    void my_fun( Out m ) {...}

    and call the function:

    my_fun(back_ins erter(my_map));

    My problem is: Is it possible to write the instruction
    loc_map["hello"].push_back(1); using the parameter m?

    Thanks
  • James Kanze

    #2
    Re: Associative containers and iterators

    On Aug 2, 2:13 pm, anonymous <n...@none.comw rote:
    I have a function that returns a map<string, vector<int.
    Inside the function I load the map with this instruction:
    loc_map["hello"].push_back(1);
    I would like to remove the output data structure and to use a
    template function with iterators.
    My idea is to use an iterator in this way:
    template <class Out>
    void my_fun( Out m ) {...}
    and call the function:
    my_fun(back_ins erter(my_map));
    My problem is: Is it possible to write the instruction
    loc_map["hello"].push_back(1); using the parameter m?
    A back_insert_ite rator always inserts at the end, using
    push_back. An associative container cannot allow the user to
    insert where he wants; it must insert where it wants. Thus, a
    back_insert_ite rator cannot be used with it.

    It's somewhat of an abuse, but you can use a normal
    insert_iterator , passing it the end iterator of the map when you
    construct it. In the case of map, of course, you'll have to
    write a map<>::value_ty pe to it. But frankly, what's wrong with
    just passing the function a reference to the map, and letting it
    do its job normally? Maps aren't containers, regardless of what
    the standard says, and trying to use them like a container is
    generally misleading.

    --
    James Kanze (GABI Software) email:james.kan ze@gmail.com
    Conseils en informatique orientée objet/
    Beratung in objektorientier ter Datenverarbeitu ng
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

    Comment

    Working...