Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 8:20 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 2 posts ] 
Author Question
 Question subject: Multimap iteration
PostPosted: Fri Nov 07, 2008 6:15 pm 
Offline
Proficient
User avatar

Joined: Sun Oct 19, 2008 3:47 pm
Posts: 281
Has thanked: 0 time
Have thanks: 1 time

Im currently working with multimap in C++. So for example i got a multimap like
this:
Code:
m.insert(pair<const char* const, int>("a", 1));
m.insert(pair<const char* const, int>("c", 2));
m.insert(pair<const char* const, int>("b", 3));
m.insert(pair<const char* const, int>("b", 4));
m.insert(pair<const char* const, int>("a", 5));
m.insert(pair<const char* const, int>("b", 6));

and i want to loop it but without repeating the same key eg: I want to print
average of each key and print like this

Code:
a = 3
b = 6.3
c = 2


My current solution is loop each key and find all other value with the same key.
So its like a loop in loop. But the problem is I print a twice, b three times,
and C once. How can I skip the repetition so it wont print the same key again?


TOP
 Profile Send private message  
Reply with quote  
 Question subject: Re: Multimap iteration
PostPosted: Fri Nov 07, 2008 6:17 pm 
Offline
Proficient
User avatar

Joined: Sun Oct 19, 2008 3:53 pm
Posts: 229
Has thanked: 0 time
Have thanks: 0 time
If there are no memory constraints you can create a temporary map of key
& valueInfo.
valueInfo can be a structure like following...


Code:
Struct valueInfo
{
int val_total;
int count
}



Now you can fill this map while traversing your original data structure
(multimap) ...

For your example it will be
Code:
("a", (6,2))
("b", (13,3))
("c", (2,1))



Once you have traversed the original data structure, you can calculate
the desired output with a single traversal of your temp map.



So it will be Loop after Loop solution instead of yours Loop in Loop


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 2 posts ] 
Quick reply


  

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 3 guests



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team