📌  相关文章
📜  C++中的std :: partial_sort_copy

📅  最后修改于: 2021-05-30 13:23:12             🧑  作者: Mango

std :: partial_sort用于对整个容器中的范围进行排序。因此,如果我们想保持原始容器不变,而只是将容器的排序后的子部分复制到另一个子容器中,则可以使用std :: partial_sort_copy

就像std :: partial_sort一样,partial_sort_copy()可以通过两种方式使用,如下所示:

  1. 使用<:比较元素

    句法:

    Template 
    RandomAccessIterator partial_sort_copy (InputIterator first, InputIterator last,
                                            RandomAccessIterator result_first,
                                            RandomAccessIterator result_last);
    
    first: Input iterator to the first element in the container.
    last: Input iterator to the last element in the container.
    result_first: Random-Access iterator pointing to the initial
    position in the destination container.
    result_last: Random-Access iterator pointing to the final
    position in the destination container.
    
    Return Value: It returns an iterator pointing to the element that 
    follows the last element written in the result sequence.
    
    // C++ program to demonstrate the use of
    // std::partial_sort_copy
    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
        vector v = { 1, 1, 3, 10, 3, 3, 7, 7, 8 }, v1(3);
      
        vector::iterator ip;
      
        // Using std::partial_sort_copy
        std::partial_sort_copy(v.begin(), v.end(), v1.begin(), v1.end());
      
        // Displaying the vector after applying
        // std::partial_sort_copy
        for (ip = v1.begin(); ip != v1.end(); ++ip) {
            cout << *ip << " ";
        }
      
        return 0;
    }
    

    输出:

    1 1 3
    

    在这里,由于我们声明v1的大小为3,因此仅存储了三个元素。

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

    句法:

    Template
     RandomAccessIterator partial_sort_copy (InputIterator first, InputIterator last,
                                             RandomAccessIterator result_first,
                                             RandomAccessIterator result_last,
                                             Compare comp);
    
    Here, first, last, result_first and result_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 returns an iterator pointing to the element that 
    follows the last element written in the result sequence.
    
    // C++ program to demonstrate the use of
    // std::partial_sort_copy
    #include 
    #include 
    #include 
    using namespace std;
      
    // Defining the BinaryFunction
    bool comp(int a, int b)
    {
        return (a < b);
    }
      
    int main()
    {
        vector v = { 1, 1, 3, 10, 3, 3, 7, 7, 8 }, v1(7);
      
        vector::iterator ip;
      
        // Using std::partial_sort_copy
        std::partial_sort_copy(v.begin(), v.end(), v1.begin(),
                               v1.end(), comp);
      
        // Displaying the vector after applying
        // std::partial_sort_copy
        for (ip = v1.begin(); ip != v1.end(); ++ip) {
            cout << *ip << " ";
        }
      
        return 0;
    }
    

    输出:

    1 1 3 3 3 7 7
    

在哪里使用呢?

  • 为了复制排序范围:因此,每当我们希望原始容器在排序后保持不变,而partial_sort的结果存储在另一个容器中时,则可以使用它。
    // C++ program to demonstrate the use of
    // std::partial_sort_copy
    #include 
    #include 
    #include 
    using namespace std;
    int main()
    {
        vector v = { 100, 45, 78, 23, 220 }, v1(5);
      
        vector::iterator ip;
      
        // Using std::partial_sort_copy
        std::partial_sort_copy(v.begin(), v.end(), v1.begin(),
                               v1.end());
      
        // Displaying the vectors after applying
        // std::partial_sort_copy
        cout << "v = ";
      
        for (ip = v.begin(); ip != v.end(); ++ip) {
            cout << *ip << " ";
        }
      
        cout << "\nv1 = ";
        for (ip = v1.begin(); ip != v1.end(); ++ip) {
            cout << *ip << " ";
        }
        return 0;
    }
    

    输出:

    v = 100 45 78 23 220
    v1 = 23 45 78 100 220
    

    因此,这里v保持不变,并且其排序形式存储在v1中。

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