Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Associative containers and iterators

Reply
Thread Tools

Associative containers and iterators

 
 
anonymous
Guest
Posts: n/a
 
      08-02-2008
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
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      08-02-2008
On Aug 2, 2:13 pm, anonymous <(E-Mail Removed)> 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:(E-Mail Removed)
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Containers of iterators vs. containers of references clark.coleman@att.net C++ 7 01-25-2008 01:37 PM
Why "associative" in associative container? desktop C++ 5 06-26-2007 07:49 AM
Sequential and Associative containers utab C++ 2 03-12-2006 03:18 PM
Tradeoffs between multi and non-multi associative containers Dave C++ 3 04-19-2004 07:48 PM
Iterators as associative containers' key =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?= C++ 2 06-26-2003 04:43 AM



Advertisments