std::partial_sort_copy in C++ Last Updated : 06 Aug, 2017 Comments Improve Suggest changes Like Article Like Report std::partial_sort is used for sorting the range within the entire container. So, if we want to keep the original container intact and just copy the sorted sub-part of the container into another one, then for that purpose, we can use std::partial_sort_copy. Just like std::partial_sort, partial_sort_copy() can be used in two ways as shown below: Comparing elements using <: Syntax: 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. CPP // C++ program to demonstrate the use of // std::partial_sort_copy #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v = { 1, 1, 3, 10, 3, 3, 7, 7, 8 }, v1(3); vector<int>::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; } Output: 1 1 3 Here, since, we declared v1 to be of size 3, so only three elements were stored in it. By comparing using a pre-defined function: Syntax: 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. CPP // C++ program to demonstrate the use of // std::partial_sort_copy #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, 1, 3, 10, 3, 3, 7, 7, 8 }, v1(7); vector<int>::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; } Output: 1 1 3 3 3 7 7 Where to use it ? For copying the sorted range: So, whenever we want the original container to remain unchanged after sorting and the resultant of partial_sort to be stored inside another container, then we can use this. CPP // C++ program to demonstrate the use of // std::partial_sort_copy #include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> v = { 100, 45, 78, 23, 220 }, v1(5); vector<int>::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; } Output: v = 100 45 78 23 220 v1 = 23 45 78 100 220 So, here v remained the same, and its sorted form is stored into v1. Comment More infoAdvertise with us Next Article Interview Preparation For Software Developers M Mrigendra Singh Improve Article Tags : Misc C++ STL cpp-algorithm-library Practice Tags : CPPMiscSTL Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like