0

As in the title, how does std::sort() gets iterators and pointers? Can someone tell how it is implemented?

std::sort(vec.begin(), vec.end())

std::sort(arr, arr + arr_size)

user5876164
  • 471
  • 3
  • 15
  • [c++ - is it possible to make function that will accept multiple data types for given argument? - Stack Overflow](https://stackoverflow.com/questions/8627625/is-it-possible-to-make-function-that-will-accept-multiple-data-types-for-given-a) – user202729 Jan 08 '19 at 15:09
  • 5
    Formally pointers *are* iterators. – HolyBlackCat Jan 08 '19 at 15:09
  • pointers _are_ random-access iterators, as they conform to all their requirements; hence, a template algorithm written to work with the requirements for random-access iterators will work with pointer just fine. – Matteo Italia Jan 08 '19 at 15:09
  • 1
    @user463035818 [There is no overload for `std::sort` that takes pointers](https://en.cppreference.com/w/cpp/algorithm/sort) – NathanOliver Jan 08 '19 at 15:12
  • @user202729 meh sorry I was a bit confused and the comments lead nowhere, ill delete them – 463035818_is_not_an_ai Jan 08 '19 at 15:13
  • 2
    Iterator is a concept. An algorithm that accepts a certain type of iterator is simply a template that expect it's template argument(s) to respect a certain set of requirements. An iterator can be *any* object that respects those requirements. Pointer just so happen (by design) to satisfy all of the requirements for being a random access iterator. – François Andrieux Jan 08 '19 at 15:14
  • @NathanOliver It's unclear what you mean. All overloads of `std::sort` take pointers. – eerorika Jan 08 '19 at 15:25
  • 1
    @eerorika I was talking to a now deleted comment. It said the function was overloaded to take pointers. My link shows this is not the case, it is just a template and it will accept pointers since they are random access iterators. If the function was actually overloaded for pointers it would have `T*` parameters. – NathanOliver Jan 08 '19 at 15:27

0 Answers0