0

In the code sort(A.begin(),A.end()); where A is defined as vector<pair<int,pair<int,int>>> A;.

If I call the sort method, then on which basis will sorting be done?

Niall
  • 30,036
  • 10
  • 99
  • 142
tryst001
  • 11
  • 6
  • 1
    Possible duplicate of [Is std::pair ordering well-defined?](http://stackoverflow.com/questions/2819245/is-stdpairint-stdstring-ordering-well-defined) – BeyelerStudios Jan 27 '16 at 12:08
  • 1
    It will sort on `std::pair::operator <`, details of which can be found [here](http://en.cppreference.com/w/cpp/utility/pair/operator_cmp). – erip Jan 27 '16 at 12:15

1 Answers1

3

It will compare using the operator < for the std::pair, specified here.

It compares the elements lexicographically on the first element first, and if they are equal, then on the second element.

Given the complex type here pair<int,pair<int,int>>, it may be a better idea to the give the std::sort algorithm a custom comparator. Make sure that the functor provided fulfils the requirements of the Compare concept, that being strict weak ordering.

Niall
  • 30,036
  • 10
  • 99
  • 142