Now, the question is, if we would like to find the first two consecutive pairs in the vector with greatest equal second, namely here pairs (3,50) and (8,50), any ideas on how we handle this?
Seeplus thanks a lot. Today your help was extremely important! One last question regarding the above in conjuction with my previous thread. If instead of using the largest smallest, I choose to use this, the change will be something like this:
Problem is that despite the fact that both now and before, I have two pairs (before largest and smallest) and (now prev and next), the code now does not compile as it considers K.first error. Any ideas why?
The part of my code (altered to included the two greatest consecutive pairs instead of largsest - smallest) in which I needed help and discussed together in my posts so far is the following:
Yep. It will! You haven't got my test for bad sec! What if v has only 1 entry - or several entries with different .second values? In these cases sec will be invalid.
See L20 of my code above.
PS. You do know you're only reading the first item from each file?
I am posting an enquiry regarding the code. I tried reading from files and store the data into a vector of pairs. However when I try to obtain the first two pairs with greatest second and then calculate K
1 2 3 4 5
constauto greatest {V.back()};
constauto first {std::partition_point(V.rbegin(), V.rend(), [&](constauto& v1) { return v1.second == greatest.second; }).base()};
constauto sec {std::next(first)};
if (sec != V.end()) constauto K {*first - *sec};
K always ends up being equal to zero. If some data in my file has a fractional part (i.e .5) I could understand that in case the pairs weren't of double type. Since I have define pairs as double, why do I get k equal to zero?
You really need to get to grips with code debugging and the debugger. If K.first is 0 then you should be able to trace why back to the data - or to a code issue.
PPS I don't know what these numbers are - but if they're close to several decimal places, then subtraction could result in a number that 'seems' to be 0. Also you shouldn't compare double numbers for equality - you should compare that their absolute difference is < a specified value.
If a and b are double then for equality:
1 2 3 4 5
constdouble epsilon {0.000001}; // Or what is required
if (abs(a - b) < epsilon) {
// a and b are consider equal here
}
Boost Math Toolkit on comparing floating-point values to see if they are tolerably close enough to each other:
. Absolute difference/error: the absolute difference between two values a and b is simply fabs(a-b). This is the only meaningful comparison to make if we know that the result may have cancellation error.
. The edit distance between the two values: i.e. how many (binary) floating-point values are between two values a and b? ... probably only useful when you know that the distance should be very small.
. The relative distance/error between two values. This is quick and easy to compute, and is generally the method of choice when checking that your results are "tolerably close" to one another. However, it is not as exact as the edit distance when dealing with small differences...