📜  C++中的std :: partial_sort

📅  最后修改于: 2021-05-30 10:08:27             🧑  作者: Mango

std :: sort用于对容器中存在的元素进行排序。其中的一种变体是std :: partial_sort ,它不是用于对整个范围进行排序,而只是对其中的一部分进行排序。

它以[first,last)范围内的元素重新排列,以使Middle之前的元素以升序排序,而Middle后面的元素则没有任何特定顺序。

可以按以下两种方式使用它:

  1. 使用<:比较元素

    句法:

    Template 
    void partial_sort (RandomAccessIterator first, RandomAccessIterator middle,
                       RandomAccessIterator last);
    
    first: Random-Access iterator to the first element in the container.
    last: Random-Access iterator to the last element in the container.
    middle: Random-Access iterator pointing to the element in the 
    range [first, last), that is used as the upper boundary for the elements 
    to be sorted.
    
    Return Value: It has a void return type, so it does not return any value.
    
    // C++ program to demonstrate the use of
    // std::partial_sort
    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
        vector v = { 1, 3, 1, 10, 3, 3, 7, 7, 8 }, i;
      
        vector::iterator ip;
      
        // Using std::partial_sort
        std::partial_sort(v.begin(), v.begin() + 3, v.end());
      
        // Displaying the vector after applying
        // std::partial_sort
        for (ip = v.begin(); ip != v.end(); ++ip) {
            cout << *ip << " ";
        }
      
        return 0;
    }
    

    输出:

    1 1 3 10 3 3 7 7 8 
    

    在这里,只有前三个元素是从第一个到中间排序的,这里的第一个是v.begin(),中间的是v.begin()+ 3,其余的没有任何顺序。

  2. 通过使用预定义函数进行比较:

    句法:

    Template
     void partial_sort (RandomAccessIterator first, RandomAccessIterator middle,
                        RandomAccessIterator last, Compare comp);
    
    Here, first, middle and last are the same as previous case.
    
    comp: Binary function that accepts two elements in the range 
    as arguments, and returns a value convertible to bool. The value 
    returned indicates whether the element passed as first 
    argument is considered to go before the second in the specific
    strict weak ordering it defines.
    The function shall not modify any of its arguments.
    This can either be a function pointer or a function object.
    
    Return Value: It has a void return type, so it does not return any value.
    
    // C++ program to demonstrate the use of
    // std::partial_sort
    #include 
    #include 
    #include 
    using namespace std;
      
    // Defining the BinaryFunction
    bool comp(int a, int b)
    {
        return (a < b);
    }
      
    int main()
    {
        vector v = { 1, 3, 1, 10, 3, 3, 7, 7, 8 }, i;
      
        vector::iterator ip;
      
        // Using std::partial_sort
        std::partial_sort(v.begin(), v.begin() + 3, v.end(), comp);
      
        // Displaying the vector after applying
        // std::partial_sort
        for (ip = v.begin(); ip != v.end(); ++ip) {
            cout << *ip << " ";
        }
      
        return 0;
    }
    

    输出:

    1 1 3 10 3 3 7 7 8 
    

在哪里可以使用?

  1. 找到最大的元素:由于使用std :: partial_sort,我们可以对容器进行部分排序,直到我们想要的任何位置。因此,如果我们只排序第一个位置并使用一个函数对象,则可以找到最大的元素,而不必对整个容器进行排序。
    // C++ program to demonstrate the use of
    // std::partial_sort
    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
        vector v = { 10, 45, 60, 78, 23, 21, 30 };
      
        vector::iterator ip;
      
        // Using std::partial_sort
        std::partial_sort(v.begin(), v.begin() + 1, v.end(),
                          greater());
      
        // Displaying the largest element after applying
        // std::partial_sort
      
        ip = v.begin();
        cout << "The largest element is = " << *ip;
      
        return 0;
    }
    

    输出:

    The largest element is = 78
    
  2. 查找最小的元素:与查找最大的元素相似,我们也可以在前面的示例中在容器中找到最小的元素。
    // C++ program to demonstrate the use of
    // std::partial_sort
    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
        vector v = { 10, 45, 60, 78, 23, 21, 3 };
      
        vector::iterator ip;
      
        // Using std::partial_sort
        std::partial_sort(v.begin(), v.begin() + 1, v.end());
      
        // Displaying the smallest element after applying
        // std::partial_sort
      
        ip = v.begin();
        cout << "The smallest element is = " << *ip;
      
        return 0;
    }
    

    输出:

    The smallest element is = 3
    

要记住的一点:

  • std :: sort()与std :: partial_sort():有些人可能会认为我们为什么要使用std :: partial_sort,因此我们可以在有限范围内使用std :: sort(),但是请记住,如果我们使用带有部分范围的std :: sort,则仅考虑该范围内的元素进行排序,而出于此目的,将不考虑该范围外的所有其他元素,而使用std :: partial_sort()时,所有元素都将进行排序考虑进行排序。
    // C++ program to demonstrate the use of
    // std::partial_sort
    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
        vector v = { 10, 45, 60, 78, 23, 21, 3 }, v1;
      
        int i;
        v1 = v;
        vector::iterator ip;
      
        // Using std::partial_sort
        std::partial_sort(v.begin(), v.begin() + 2, v.end());
      
        // Using std::sort()
        std::sort(v1.begin(), v1.begin() + 2);
      
        cout << "v = ";
        for (i = 0; i < 2; ++i) {
            cout << v[i] << " ";
        }
      
        cout << "\nv1 = ";
        for (i = 0; i < 2; ++i) {
            cout << v1[i] << " ";
        }
      
        return 0;
    }
    

    输出:

    v = 3 10
    v1 = 10 45
    

    说明:在这里,我们在v上应用了std :: partial_sort,在v1上应用了std :: sort,直到第二个位置。现在,您可以了解std :: sort仅对给定范围内的元素进行排序,而partial_sort考虑了整个容器,但仅对前两个位置进行了排序。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”