Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

const std::vector<int> v = {5, 7, 3, 6, 5, 4, 7, 8, 5, 6};

auto low = std::lower_bound( v.begin(), v.end(), 7);
auto high = std::upper_bound( v.begin(), v.end(), 7);
std::cout << low - v.begin() << " " << high - v.begin();

So when I try to compile this code using the clang++ compiler on my Mac , it returns the output as

10 10

which implies as v.end() for both high and low although low should be = 1 and high =7 (the number 8). What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
352 views
Welcome To Ask or Share your Answers For Others

1 Answer

std::lower_bound and std::upper_bound requires that the range is "sorted" (partitionned according to predicate and given value in fact), which is not your case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...