How to create a sorted copy of part of a vector in C++

1 Answer

0 votes
#include <algorithm> 
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> vec = { 4, 9, 19, 16, 24, 21, 10, 3 }, v(5);

    partial_sort_copy(vec.begin(), vec.end(), v.begin(), v.end());

    for (auto const &n: v) {
        std::cout << n << " ";
    }
}




/*
run:

3 4 9 10 16 

*/

 



answered Jun 9, 2023 by avibootz
...