0

I have some file like following i need to remove the a multimap

 3KHJG 76  238
 3KHJG 86  238
 3GDMB 31  244
 3GDMB 31  247
 3GDMB 31  251
 3KOFA 21  336
 3KOFA 36  263

....

i need to get a file such as:

 3KHJG 81  238
 3GDMB 31  247

.....

// which keep the string key and calculate the avg of the next columns;

i have

 typedef struct S{
   int v1;
   int v2;
   ...
   }struct1;

struct1 s1;                 
//I parsed the old file and put them into a multimap and a set<string>;
multimap <string, s1> m;
multimap <string, s1>::iterator i;
set <string> pa_set;
int sumv1, sumv2;
int avgv1, avgv2;
for (set<string>:: iterator ip= pa_set.begin(); ip !=pa_set.end(); ip++)
{

  multimap <string, struct1> ::iterator i = m.find(*ip);

       int cnt=m.cout(*ip);
       if (ip != m.end())
      { 
        v1 =i->second.v1;
        v2 =i->second.v2;
        sumv1+=v1;
        sumv2+=v2;
        i++;
      }

      //calculate the avgv1 and avgv2;
}

however, if I used the if (){} it looks like i only iterator once? how can I go through all the interators fullfill

    multimap<string,struct1> ::iterator i = m. find(*ip) && i != m.end() ;        

Thanks a lot!

user1830108
  • 195
  • 1
  • 15

2 Answers2

1

You can use the equal_range function that will give you pair of iterators to the range from which you'll compute the averages.

jpalecek
  • 47,058
  • 7
  • 102
  • 144
  • is the m.equal_range(key) gives the lower and upper boundary? if my m.count (key) actually is a number from 10-200? how i go through all the items in between the lower and upper boundary ? is there a way to loop through every iterator one by one. The only technique i know is erase each found key and cut down m.count (key) and use a while (m.count > 0 && condition2) loop, but I think there may be other methods...Thanks – user1830108 Jan 15 '13 at 21:53
  • http://stackoverflow.com/questions/247818/stlmultimap-how-do-i-get-groups-of-data.....Thanks! I think i did not fully realize how to implement the equal_range(key )until i see the above post...yes..you are right! the equal_range could be a choice :-) – user1830108 Jan 18 '13 at 17:02
0

You need to use equal_range for this, like:

equal_range(*ip);

Check this example here.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Satish
  • 713
  • 1
  • 5
  • 18