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:
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?
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?
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).