2016-02-28 15 views
5

Sto usando std::max_element(vec), ma da quello che posso dire, restituisce l'indice più piccolo se due "più grandi" indici sono uguali.Come posso trovare l'indice del valore più alto in un vettore, in modo predefinito sull'indice più grande se ci sono due indici "maggiori"?

Esempio:

vector<int> v = {1, 2, 3, 4, 5, 3, 3, 2, 5}; 

std::max_element(v) avrebbe riferimento v[4], ma ai fini del mio progetto ho bisogno di fare riferimento v[8] invece. Quale sarebbe il modo migliore per farlo?

+1

'max_element' con' reverse_iterator's? (Stai scrivendo pseudo-codice o stai già utilizzando la libreria di gamma di Eric Niebler? Non esiste attualmente lo standard 'std :: max_element' che prende solo un' std :: vector'.) – BoBTFish

+0

Puoi cercare dal primo risultato ottenuto . –

+0

@BoBTFish grazie! Vedrò questo. – PanicSkittle

risposta

8

È possibile utilizzare questo

max_element(v.rbegin(), v.rend()); 

per fare riferimento al più grande indice del più grande valore.

Ad esempio,

#include "iostream" 
#include "vector" 
#include "algorithm" 
using namespace std; 

int main() 
{ 
    vector<int> v = {1, 2, 3, 4, 5, 3, 3, 2, 5}; 
    *max_element(v.rbegin(), v.rend())=-1; 
    for (auto i: v) cout << i << ' '; 
} 

produce uscita

1 2 3 4 5 3 3 2 -1 

Il metodo di cui sopra restituisce un iteratore inverso, come sottolinea @BoBTFish. Per ottenere un iteratore in avanti, si potrebbe fare questo:

#include "iostream" 
#include "vector" 
#include "algorithm" 
using namespace std; 

int main() 
{ 
    vector <int> v = {1, 2, 3, 4, 5, 3, 3, 2, 5}; 
    reverse_iterator < vector <int> :: iterator > x (max_element(v.rbegin(), v.rend())); 
    vector <int> :: iterator it=--x.base(); // x.base() points to the element next to that pointed by x. 
    *it=-1; 
    *--it=0; // marked to verify 
    for (auto i: v) cout << i << ' '; 
} 

produce un output

1 2 3 4 5 3 3 0 -1 
      ^

Si può notare che l'iteratore it è un iteratore in avanti.

+0

Tieni presente che questo ti dà un 'reverse_iterator' sull'elemento, che potresti voler riconvertire con il tipo di iteratore" corretto "(' std :: vector :: iterator'). Penso che migliorerebbe la tua risposta se dimostrassi come farlo. – BoBTFish

4

E 'molto facile fare la propria funzione:

/* Finds the greatest element in the range [first, last). Uses `<=` for comparison. 
* 
* Returns iterator to the greatest element in the range [first, last). 
* If several elements in the range are equivalent to the greatest element, 
* returns the iterator to the last such element. Returns last if the range is empty. 
*/ 

template <class It> 
auto max_last(It first, It last) -> It 
{ 
    auto max = first; 
    for(; first != last; ++first) { 
     if (*max <= *first) { 
      max = first; 
     } 
    } 
    return max; 
} 
+0

Si potrebbe fare in modo che questo segua lo stile degli algoritmi standard prendendo un 'Comparator' che segue un rigoroso ordine debole, quindi usando' if (! Cmp (* first, * max)) {max = first; } '. Sebbene gli algoritmi standard in genere abbiano una versione che non accetta un comparatore e che esegue il default su '<' (che non è esattamente la stessa cosa di default a 'std :: less ', che può essere specializzato). – BoBTFish