Question

I have a std::vector of double values. Now I need to know if two succeeding elements are within a certain distance in order to process them. I have sorted the vector previously with std::sort.
I have thought about a solution to determine these elements with std::find_if and a lambda expression (c++11), like this:

std::vector<std::vector<double>::iterator> foundElements;
std::vector<double>::iterator foundElement;
while (foundElement != gradients.end())
{
    foundElement = std::find_if(gradients.begin(), gradients.end(),
                 [] (double grad)->bool{
        return ...
    });
    foundElements.push_back(foundElement);
}

But what should the predicate actually return? The plan is that I use the vector of iterators to later modify the vector.
Am I on the right track with this approach or is it too complicated/impossible? What are other, propably more practical solutions?

EDIT: I think I will go after the std::adjacent_find function, as proposed by one answerer.

Was it helpful?

Solution

Read about std::adjacent_find.

OTHER TIPS

Can you enhance the grammar of your question?

"I have a std::vector with different double values. Now I need to know if two succeeding ones (I have sorted the vector previously with std::sort) are within a certain distance to process them)."

Do you imply that each element of a vector of type double is a unique value? If that's the case, can it be reasonably inferred that your goal to find the distance between each of these elements?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top