Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
c++

What is the partial_sort_copy() function in C++?

Sumit Sharma

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

In this shot, we will discuss how to use the partial_sort_copy() function in C++.

The partial_sort_copy() function is used to create a copy of the sorted part of a sorted range.

To use the partial_sort_copy(), you must import the <algorithm> library, as shown below:

#include <algorithm>

Parameters

The partial_sort_copy() function takes the following parameters:

  • begin: A random access iterator that points to the beginning position of the existing container.
  • end: A random access iterator that points to the ending position of the existing container.
  • begin_new: A random access iterator that points to the beginning position of a new container in which elements will be copied from the existing container.
  • end_new: A random access iterator that points to the ending position of the new container in which elements will be copied from the existing container.
  • pred: A user-defined function that returns true if the two arguments passed are in orderfollows strict weak ordering.

Return value

The partition_sort_copy() function returns an iterator that points to the last element in the copy of the sorted subpart of a range.

Code

Let’s look at the code snippet below.

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool pred(int x, int y)
{
return (x<y);
}
int main()
{
vector<int> vect = { 12, 15, 9, 7, 84, 74 }, v(4);
vector<int>::iterator ip;
partial_sort_copy(vect.begin(), vect.end(), v.begin(), v.end());
cout<<"The resultant copy vector after using partial_sort_copy() is: ";
for (ip = v.begin(); ip != v.end(); ip++)
cout << *ip << " ";
cout << endl;
return 0;
}
Using partial_sort_copy() function in C++

Explanation

  • In lines 1 to 3, we imported the required libraries.

  • In lines 6 to 9, we defined a function to check whether the first element in the argument is less than the second element in the argument or not.

  • In line 11, we made a main function.

  • In line 13, we initialized a vector called vect, and declare another vector called v to store the copy.

  • In line 14, we declared an iterator to a vector.

  • In line 15, we used partial_sort_copy() to sort and create a copy of a sub-range of vect.

  • In line 17, we displayed a message regarding the upcoming copy of the modified sub-range of the vector.

  • In lines 18 and 19, we used a loop to access and display the elements with the help of an iterator of the modified vector.

In this way, we can use the partial_sort_copy() function in C++.

RELATED TAGS

communitycreator
c++

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring