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

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

c++ - lower_bound/upper_bound on unsorted vector

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...