Binary search with returned index in STL?

前端 未结 6 1855
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  萌比男神i
    2021-01-12 12:06

    using STL we can find the index

    vector vec{1,2,3,4,5,6,7,8,9} ;
    vector :: iterator index;
    index=lower_bound(vec.begin(),vec.end(),search_data);
    return (index-vec.begin());
    

提交回复
热议问题