0

After using make_pair, I got some pairs namely pairToCount

in this format (element 1 element 2)= # of frequent in the Db:

(1 2) = 1
(1 3) = 1
(1 4) = 1
(2 3) = 2
(2 4) = 3
(2 5) = 1
(3 4) = 2
(5 4) = 1

I want to do the following for each number (1,2,3,4,5):

First, check each pair has number 1 ==> then sum its frequent for example: 1 is exist in the following pairs: (1 2), (1 3), (1 4)==> the sum of there frequent is =1+1+1=3

do the same for 2, 3, 4,5

2 is in (1 2), (2 3), (2 4),(2 5)==> the sum of there frequent is =1+2+3+1=7

3 is in (1 3), (2 3), (3 4)==> the sum of there frequent is =1+2+2=5

and so on. This is the code I wrote it.

int sum = 0;
    int found;
    for (const auto& p1 : pairToCount)
    {   
        int r = p1.first.first;
        std::cout << " (" << r;

        for (const auto& p2 : pairToCount)
        {
            if (r == p2.first.first)
            {
                sum += p2.second;
                found = p2.first.second;
            }

            else if (r == p2.first.second)
            {
                sum += p2.second;
                found = p2.first.first;
            }
            else
                exit;
            std::cout << "," << found ;
        }
        std::cout <<  ") = "<< sum << "\n ";
        sum = 0;
    }

I got duplication printing the last element and the same test + since there is no pair start with 4, the code doesn't work in this case.

This the result:

 (1,2,3,4,4,4,4,4,4) = 3
 (1,2,3,4,4,4,4,4,4) = 3
 (1,2,3,4,4,4,4,4,4) = 3
 (2,1,1,1,3,4,5,5,5) = 7
 (2,1,1,1,3,4,5,5,5) = 7
 (2,1,1,1,3,4,5,5,5) = 7
 (3,5,1,1,2,2,2,4,4) = 5
 (5,4,4,4,4,4,2,2,4) = 2

I just learn make_pair, and spent 4 hours reading about it, to see if there is any example or similar question can guide me but no luck!

Joe
  • 41,484
  • 20
  • 104
  • 125
NHA
  • 9
  • 2
  • 8

1 Answers1

2

You can simplify your problem a lot by using a map (or unordered_map if you have C++11 or from boost).

The idea would be to iterate once over your list of pairs/frequency, and store the partial sums in the map for each key. You can then print out the sums in a second loop.

#include <map>

// ...

std::map<int,int> sums;
for (const auto& p1 : pairToCount)
  sums[p1.first.first]  += p1.second;
  sums[p1.first.second] += p1.second;
}

for (const auto& k : sums) {
  // print what you want
}
Mat
  • 202,337
  • 40
  • 393
  • 406
  • thank you, this code seems smart, but I don't have that, I'm using visual stdio 2013/vc++. since this is the first time I hear about boost and c++11, I read some related topics, (ex. http://stackoverflow.com/questions/17440810/how-do-i-build-boost-with-new-visual-studio-2013-preview/17440811#17440811), and it sound complicated for me. – NHA Jan 19 '14 at 23:13
  • actually, i just change map[p1.first.first] += p1.second; map[p1.first.second] += p1.second; **To** sums[p1.first.first] += p1.second; sums[p1.first.second] += p1.second; and it works. thank u – NHA Jan 19 '14 at 23:32
  • Ah yes, typo. Sorry about that. – Mat Jan 20 '14 at 04:27