retrieving multiple elements from a vector in one pass

I want to be able to sort a class based on the size of a vector that lives inside. I want for the algorithm to return me 5 elements with the highest vector string lengths. I can do it in a way that is not very efficient:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
for( int i = 0; i < _notesProducts.size();i++){
        
        while (newProducts.size() < 5) {// we need to make sure that we don't add more than 5 products
            
            if ( _notesProducts[i].listedNotes.size() >= 4 ){
                
                newProducts.push_back(_notesProducts[i]);
                
            }
        }
    }
    
    for( int i = 0; i < _notesProducts.size();i++){
        
        while (newProducts.size() < 5) {// we need to make sure that we don't add more than 5 products
            
            if ( _notesProducts[i].listedNotes.size() >= 3 ){
                
                newProducts.push_back(_notesProducts[i]);
                
            }
        }
    }
    for( int i = 0; i < _notesProducts.size();i++){
        
        while (newProducts.size() < 5) {// we need to make sure that we don't add more than 5 products
            
            if ( _notesProducts[i].listedNotes.size() >= 2 ){
                
                newProducts.push_back(_notesProducts[i]);
                
            }
        }
    }


Is there any type of iterator that allows me to do that? I know that std::find() does it but only for one element. Any suggestions?
Last edited on
Are you allowed to change order of elements in vector?
If so, nth_element() with custom comparator will do the trick:
http://en.cppreference.com/w/cpp/algorithm/nth_element
I am allowed, yes. But wouldn't that only return one element based on the examples that is provided. How do I use it when I compare different things at the same time? I think I'm just having a hard time trying to picture the use of the method within one loop. Could you provide an example?

Thanks!
an example of populating another vector with 5 largest values from another vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<algorithm>
#include<vector>
#include<iterator>

int main()
{
    std::vector<int> original {1, 6, 2, 8, 4, 8, 3, 7, 2, 3};
    std::vector<int> result;
    std::nth_element(original.begin(), original.begin() + 4, original.end(),
                     [](int l, int r){ return r < l; });
    result.assign(original.begin(), original.begin() + 5);
    for(auto i: result)
        std::cout << i << ' ';
}
6 7 8 8 4
http://coliru.stacked-crooked.com/a/a038bbc8d77b961a
That's exactly what I needed. Thanks so much!
@MiiNiPaa

One last question. In this case, what is the second parameter for? It seems that if I change it for original.begin() + 1. Or any other number it doesn't make a difference. What is the second one checking for?

Read the link I provided before. It is an iterator to element you want to be in exact place it belongs. All elements before and after will be partitioned by their relation with it (larger to the left, smaller to the right in this case).
Topic archived. No new replies allowed.