Question

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!

Was it helpful?

Solution

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

OTHER TIPS

You need to use equal_range for this, like:

equal_range(*ip);

Check this example here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top