STD::C++中的PrimalIsRoad排序

排序 用于对容器中的元素进行排序。其中一个变种是 部分排序 ,它不用于对整个范围进行排序,而仅用于对其中的一个子部分进行排序。

null

它重新排列[第一,最后]范围内的元素,使中间之前的元素按升序排序,而中间之后的元素不按任何特定顺序排列。

它有两种使用方式,如下所示:

  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 <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    int main()
    {
    vector< int > v = { 1, 3, 1, 10, 3, 3, 7, 7, 8 }, i;
    vector< int >::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 <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    // Defining the BinaryFunction
    bool comp( int a, int b)
    {
    return (a < b);
    }
    int main()
    {
    vector< int > v = { 1, 3, 1, 10, 3, 3, 7, 7, 8 }, i;
    vector< int >::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 <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main()
    {
    vector< int > v = { 10, 45, 60, 78, 23, 21, 30 };
    vector< int >::iterator ip;
    // Using std::partial_sort
    std::partial_sort(v.begin(), v.begin() + 1, v.end(),
    greater< int >());
    // 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 <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main()
    {
    vector< int > v = { 10, 45, 60, 78, 23, 21, 3 };
    vector< int >::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()vs std::partial_sort(): 你们中的一些人可能会想,为什么我们要使用std::partial_sort,我们可以在有限的范围内使用std::sort(),但是请记住,如果我们将std::sort与partial range一起使用,那么只有在该范围内的元素才会被考虑排序,而在该范围之外的所有其他元素都不会被考虑排序,而在std::partial_sort()中,将考虑所有元素进行排序。

    // C++ program to demonstrate the use of
    // std::partial_sort
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main()
    {
    vector< int > v = { 10, 45, 60, 78, 23, 21, 3 }, v1;
    int i;
    v1 = v;
    vector< int >::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 << "v1 = " ;
    for (i = 0; i < 2; ++i) {
    cout << v1[i] << " " ;
    }
    return 0;
    }

    
    

    输出:

    v = 3 10
    v1 = 10 45
    

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

本文由 辛格先生 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享