why did binary_search() false when u looking for 0

in vector stl

1 Like

please include the code you are considering

1 Like

vector v={10,20,30,0};

if(binary_search(v.begin(),v.end(),0))

cout<<â€śtrueâ€ť;

else

cout<<â€śfalseâ€ť;

out put is false

why i am getting false

the binary_search function requires that the vector is sorted

You can use â€śfindâ€ť if thatâ€™s not the case

1 Like

thk

binary_search works on a sorted container