Binary vs unary function predicate

Dec 21, 2009 at 7:50pm
1
2
3
4
5
6
7
8
9
10
bool personSortCriterion(const Person& p1, const Person& p2)
{
  return p1.lastname() < p2.lastname();
}

int main()
{
  deque<Person> coll;
  sort(coll.begin(), coll.end(), personSortCriterion);
}


Here personSortCriterion is a binary predicate. It takes p1 and p2 as arguments. sort() function calls it and passes p1 and p2.

1) How does sort() function pass arguments to the function predicate? Does it start from beginning two iterators and keep on incrementing monotonously until the end is reached?

2) If personSortCriterion happened to be a unary predicate (although it doesn't make sense, still for the purpose of understanding), would sort() function pass only one argument to it? Is so, does it mean sort() function make argument passing decision dynamically?

Thanks!
Last edited on Dec 21, 2009 at 7:50pm
Dec 21, 2009 at 8:52pm
1. No, sort requires random access iterators and uses those to its full advantage. It may compare any two items between first and last.
2. A program that passed a unary function as "comp" to std::sort() would not compile. In any case, argument passing is not done dynamically. It is done at compile time using templates and argument-dependent lookup (ADL).
Topic archived. No new replies allowed.