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)
Asked
Active
Viewed 311 times
-5

user5132646
- 13
- 2
-
I have omitted the part of code where we ask user to input and make pair to make pairs – user5132646 Jul 30 '15 at 21:07
-
What is your question, are you asking for someone to write sort for you? – Fantastic Mr Fox Jul 30 '15 at 21:14
-
Also why dont you just use a `map` or a `set`? – Fantastic Mr Fox Jul 30 '15 at 21:14
-
No i just want to know if i apply the sort on a pair which stores two integers like (1,6)(8,7)(6,10) then what would be the resulting pair.Will it get sorted on 1,8,6 or 6,7,10 . – user5132646 Jul 30 '15 at 21:16
-
Yes i tried but could not understand that's why i am asking it on stackoverflow – user5132646 Jul 30 '15 at 21:19
-
Ok, so how about you show us what you tried, did you write some compilable code that performed a sort? – Fantastic Mr Fox Jul 30 '15 at 21:20
-
possible duplicate of [How do I sort a vector of pairs based on the second element of the pair?](http://stackoverflow.com/questions/279854/how-do-i-sort-a-vector-of-pairs-based-on-the-second-element-of-the-pair) – juanchopanza Jul 30 '15 at 21:27
-
Wait Ben i am writing an answer to it.Got it – user5132646 Jul 30 '15 at 21:30
2 Answers
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).

user5132646
- 13
- 2