《條目三十一:瞭解各類與排序有關的選擇》

《條目三十一:瞭解各類與排序有關的選擇》

  • 1.sort

    template<class _RanIt,class _Pr> inline
    void sort(const _RanIt _First, const _RanIt _Last, _Pr _Pred)
    全排序,整個容器排序。算法

  • 2.partial_sort

    template<class _RanIt,class _Pr> inline
    void partial_sort(_RanIt _First, _RanIt _Mid, _RanIt _Last, _Pr _Pred)
    設定排序區間大小,即設定局部全排序排序

  • 3.nth_element

    template<class _RanIt,class _Pr> inline
    void nth_element(_RanIt _First, _RanIt _Nth, _RanIt _Last, _Pr _Pred)
    選出符合條件的前n個,這n個不排序element

  • 4.partition

    template<class _FwdIt,class _Pr> inline
    _FwdIt partition(_FwdIt _First, const _FwdIt _Last, _Pr _Pred)
    對整個容器分區,根據符合的條件分區。返回的迭代器指向第一個不符合條件的元素。資源

1, 2, 3, 4都要求是隨機迭代器,因此只能應用在vector,string,deque。get

  • 5.stable_sort/stable_partial

    用於和目的於上面是一致的,只不過,這兩個是穩定的排序,排序先後元素的先後順序不會被打亂。

如何選擇排序算法?

選擇排序算法string

消耗資源的順序(從少到多)

  • 1.partition
  • 2.stable_partial
  • 3.nth_element
  • 4.partial_sort
  • 5.sort
  • 6.stable_sort
相關文章
相關標籤/搜索