mysskin's blog

By mysskin, 9 years ago, In English

i want to binary search an array + i wanted to count the number of occurances in the array if element existed i wrote this method is there any problem with this

let e be the element to be searched and t be the vector

vectort;

int ans=0; sort(t.begin(),t.end());

if( binary_search(t.begin(),t.end(),e) ){

ib=lower_bound(t.begin(),t.end(),e);
ie=upper_bound(t.begin(),t.end(),e);
ans=distance(ib,ie);

}

ans contains the number of occurances of e

will this work fine???

  • Vote: I like it
  • -4
  • Vote: I do not like it