How to sort part of a vector in descending order with C++

1 Answer

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

int main()
{
    std::vector<int> v = { 3, 5, 7, 9, 6, 12, 8, 99, 2, 1, 0 };
    
    int first_4 = 4;
    
    partial_sort(v.begin(), v.begin() + first_4, v.end(), std::greater<int>());
 
    for(auto const& n : v)
        std::cout << n << " ";

    return 0;
}



/*
run:
       
99 12 9 8 3 5 6 7 2 1 0 
       
*/

 

 



answered Dec 2, 2021 by avibootz

Related questions

...