473,320 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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

Thanks
Aug 2 '08 #1
1 1226
On Aug 2, 2:13 pm, anonymous <n...@none.comwrote:
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:ja*********@gmail.com
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
Aug 2 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ney André de Mello Zunino | last post by:
Hello. A non-modifying algorithm I implemented uses two associative containers from STL: set and map. The elements on those containers are supposed to refer to actual elements which lie on...
1
by: Dave | last post by:
Hello all, Why is the term "associative" containter used to describe std::set<>, std::map<>, std::multiset<> and std::multimap<>? I always had the impression that it is related to the fact that...
4
by: Merlin | last post by:
Hi, I am a C++ developer and would like to implement container classes for various types of objects. I use MS Visual C++ to compile my code. Nevertheless I like to write code that is independent...
8
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
2
by: bob | last post by:
Hi, Given: 1) Custom containers that have nothing to do with vector, deque, list, map etc, 2) a custom version of new and delete defined for these containers, customNew and customDelete,...
23
by: Sanjay Kumar | last post by:
Folks, I am getting back into C++ after a long time and I have this simple question: How do pyou ass a STL container like say a vector or a map (to and from a function) ? function: ...
1
by: yonil | last post by:
I hope this is the correct group for this... I'm currently implementing the TR1 associative containers according to specification found in...
5
by: desktop | last post by:
set, map, multiset and multimap are all associative containers. But what does the word "associative" have to do with these 4 containers?
3
by: subramanian100in | last post by:
I am copying the following lines as it is, from Stanley Lippman's C++ Primer 4th edition, page 418(First paragraph). It says: "Although the map and set types provide bidirectional iterators, we...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.