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 vectorarray 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 vectorarray of Pair objects, let's say arrand return a Pair object, say sum, where sum.first represents the sum of arrifirst and sum.second represents sum of arrisecond le i pairs;
pairs.pushbackPair; pairs.pushbackPair; pairs.pushbackPair;
Old order
std::sort pairsbegin pairs.end;
New order
Pair sum std::accumulatepairsbegin pairs.end Pair; sum
vector result Pair;
std::adjacentdifferencepairsbegin pairs.end result.begin; result
Constraints
number of pairs
pairObj.first, pairObj.second
Sample Input For Custom Testing
STDIN
number of pairs n
elements in a Pair m
first Pair
second Pair
third Pair
Sample Output
Explanation
Here n
pairs
Sorted descending pairs
std::accumulate returns sum
Finally result for std::adjacentdifference
Sample Input For Custom Testing
Sample Output
Explanation
Here n
pairs
Sorted pairs
std::accumulate returns sum
Finally result for std::adjacentdifference
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
