473,666 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Aug 2 '08 #1
1 1240
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:ja******* **@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
Aug 2 '08 #2

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

Similar topics

2
2377
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 another, bigger container. I had the definition of my auxiliary containers based on pointers, but considered using iterators instead. I know iterators might become invalid upon certain operations, but as I said, the algorithm is non-modifying. This...
1
1356
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 elements in these containers are sorted. However, the (soon-to-be standard) hash-based containers are also considered associative. In these containers, element order is determined purely by the hash function and the insertion order, not by any...
4
3537
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 from MFC and intentionally I am trying to avoid inheriting from CList and CArray classes. Recently, I became familiar with iterators and have read many articles including the GOF description on what they help to achieve. My understanding so far...
8
3944
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 example: class A { public: const vector<int>& getTable() { return m_table; }
2
1593
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, say. When an item is to be inserted/removed into/from these custom containers, customNew and customDelete is called.
23
6527
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: vector<string> tokenize(string s){
1
1799
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 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1745.pdf. Now I'm not sure how to interpret a requirement relating to exception safety. It says in 6.3.1.1: "For unordered associative containers, if an exception is thrown by any operation other than the container's hash function from within...
5
1876
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
1531
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 can use only a subset of the algorithms on associative containers. The problem is that key in an associative container is const. Hence, any algorithm that writes to elements in the sequence cannot be used on an associative container. We may use...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7385
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.