Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,990 questions

51,935 answers

573 users

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

1 Answer

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

 

 



answered Dec 2, 2021 by avibootz

Related questions

1 answer 143 views
1 answer 168 views
1 answer 185 views
1 answer 140 views
1 answer 105 views
1 answer 128 views
1 answer 159 views
...