Associative containers and iterators

Discussion in 'C++' started by anonymous, Aug 2, 2008.

  1. anonymous

    anonymous Guest

    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_inserter(my_map));

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

    Thanks
    anonymous, Aug 2, 2008
    #1
    1. Advertising

  2. anonymous

    James Kanze Guest

    On Aug 2, 2:13 pm, anonymous <> wrote:
    > 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_inserter(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_iterator 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_iterator 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_type 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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Aug 2, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

    Iterators as associative containers' key

    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=, Jun 26, 2003, in forum: C++
    Replies:
    2
    Views:
    770
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Jun 26, 2003
  2. Dave
    Replies:
    3
    Views:
    390
    Kevin Goodsell
    Apr 19, 2004
  3. utab
    Replies:
    2
    Views:
    476
    Daniel T.
    Mar 12, 2006
  4. desktop
    Replies:
    5
    Views:
    383
    James Kanze
    Jun 26, 2007
  5. Replies:
    7
    Views:
    553
    Pete Becker
    Jan 25, 2008
Loading...

Share This Page