Binary search with returned index in STL?

前端 未结 6 1813
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-12 11:42

I need a binary search function.

I couldn\'t find any function in the standard library that will return the index of the found item, and if it wasn\'t found,

6条回答
  •  情话喂你
    2021-01-12 11:57

    I'm quite certain the standard library doesn't include anything to do precisely what you're asking for.

    To get what you want, you'll probably want to start from std::lower_bound or std::upper_bound, and convert the iterator it returns into an index, then complement the index of the value wasn't found.

提交回复
热议问题