-5

I am writing a code for activity schedule problem. If i store the starting time and finishing time in an array of pairs like pair<int,int>p[10] and after this i need to sort the activities as per finishing time with increasing finishing time,omitted the part where it asks user to input and then make pair for simplicity,hope it won't be a problem for the guy to answer so i apply sort(p,p+n) where n means no of activities ,i don't get like will it sort all starting activities this way or the finishing activities this way.So this is the code.pair <int,int>p[10] Sort(p,p+6)

2 Answers2

1

When sorting pair, the first items are compared and only when they are equal, the second ones are compared. So this won't sort it by the finishing time but by the starting time. But sort can have a third argument which is a method used for sorting (it returns whether the first argument is before the the second). Pass there a lambda that will compare second items (finishing time).

StenSoft
  • 9,369
  • 25
  • 30
0

It sorts the activities in the increasing order of their starting time i.e the first elements in every pair is compared to the first element in second pair and so on. .Suppose (7,9),(0,10),(4,5),(8,9),(4,10),(5,17) are the starting time and finishing time then after sorting the result would be (0,10)(4,5),(4,10),(5,17),(7,9),(8,9).