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!