mercredi 1 mars 2017

Find the last non-zero element in a std::vector

I'm trying to find the index of the last non-zero element in a std::vector<double>. If the last element in the vector is non-zero then it should return the index of that last element.

I believe I can use std::find_if_not, reverse iterators and std::distance, based on this:

std::find_if_not(amounts.rbegin(), amounts.rend(), 0.0)

where amounts is a std::vector<double>, but I'm having difficulty in combining this with std::distance and a forward iterator amounts.begin().

Also, is there a way I can introduce a predicate to compare on, say a tolerance of 1e-8?

I'm using C++11.

Aucun commentaire:

Enregistrer un commentaire