c++ - How to find a lower bound in a sorted vector -


i'm pretty new c++ , not understand concepts of stl library, bear me. wrote following code snippet (pasted below) find lower_bound in sorted vector. although code works fine in release mode, asserts in debug mode (vstudio-8). believe because less_equal<int> not strictly weak ordering .

from following thread: stl ordering - strict weak ordering

i sort of understand weak ordering imposed stl, i'm still not clear why?

in case below need use less_equal<int> since i'm trying find nearest element given value in sorted vector.

is code snippet below valid? also, there better way it? insights/references weak , partial ordering helpful.

int main() {    vector<int> dest;   for(int = 0;i <6;i++) {       dest.push_back(i);   }    vector<int>::iterator =    std::lower_bound(dest.begin(),dest.end(),4,less_equal< int >());    return 1;  } 

the stl uses strict weak orderings because given swe (let's denote <), can define 6 of relational operators:

x <  y      iff     x <  y x <= y      iff   !(y <  x) x == y      iff   !(x <  y || y <  x) x != y      iff    (x <  y || y <  x) x >= y      iff   !(x <  y) x >  y      iff     y <  x 

as problem you're trying solve, if want value close possible target value, don't need use less_equal here. rather, use lower_bound iterator smallest element bigger value you're looking (using default < comparison on integers), compare value value before (assuming, of course, both these values exist!) value lower_bound smallest element least large x , element before value largest value no greater x, 1 of 2 must closest.

as why program asserting, it's quite possible it's due fact <= not strict weak ordering, can't that. changing using above approach should fix unless problem other source.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -