Question: Design a C + + class called Pair which should: have two public integer member variables: first and second. provide a constructor that accepts two

Design a C++ class called Pair which should:
have two public integer member variables: first and second.
provide a constructor that accepts two integers to initialize the first and second variables.
be compatible with the std::sort algorithm for sorting a vector/array of Pair objects in the reverse order of std::pair.
be compatible with the std::accumulate algorithm to find the sum of a vector of Pair objects.
std::accumulate should take in a vector/array of Pair objects, let's say arrand return a Pair object, say sum, where sum.first represents the sum of arr[i].first and sum.second represents sum of arr[i].second (0\le i pairs;
pairs.push_back(Pair(1,4)); pairs.push_back(Pair(2,4)); pairs.push_back(Pair(2,3));
// Old order ={{1,4},{2,4},{2,3}}
std::sort (pairs.begin(), pairs.end());
// New order ={{2,4},{2,3},{1,4}}
Pair sum = std::accumulate(pairs.begin(), pairs.end(), Pair(0,0)); // sum ={2+2+1,4+3+4}={5,11}
vector result(3, Pair(0,0));
std::adjacent_difference(pairs.begin(), pairs.end(), result.begin()); // result ={{2,4},{0,1},{-1,1}}
Constraints
1<= number of pairs <=1000
1<= pairObj.first, pairObj.second <=1000
Sample Input For Custom Testing
STDIN
3
number of pairs n =3
2
elements in a Pair m =1
12
first Pair {1,2}
13
second Pair {1,3}
21
third Pair {2,1}
Sample Output
21
13
12
46
21
-12
0-1
Explanation
Here n =3,
pairs =[{2,1),(1,3},{1,2}]
Sorted (descending) pairs =[{2,1),(1,3),(1,2}].
std::accumulate returns sum ={2+1+1,1+3+2}={4,6}.
Finally result for std::adjacent_difference =[{2-0,1-0),(1-2,3-1),(1-1,2-3}]=[{2,1},{-1,2},{0,-1)].
Sample Input For Custom Testing
2
2
13
21
Sample Output
21
13
34
29
13
1-2
Explanation
Here n =2,
pairs =[{1,3},{2,1}]
Sorted pairs =[{2,1},{1,3}].
std::accumulate returns sum ={3,4}.
Finally result for std::adjacent_difference =[{1,3},{1,-2}]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!