bsearch

Trouble using bsearch with an array of strings

和自甴很熟 提交于 2019-12-21 17:03:23
问题 I am getting some confusing behaviour trying to use the c builtin bsearch on an array of strings in C. Here is the code. I know you can use the builtin strcmp for searching arrays of strings, but I included myStrCmp for debugging purposes because I didn't know why it wasn't working. const char *stateNames[] = {"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas",

Trouble using bsearch with an array of strings

喜夏-厌秋 提交于 2019-12-21 17:01:37
问题 I am getting some confusing behaviour trying to use the c builtin bsearch on an array of strings in C. Here is the code. I know you can use the builtin strcmp for searching arrays of strings, but I included myStrCmp for debugging purposes because I didn't know why it wasn't working. const char *stateNames[] = {"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas",

Ruby 2.0.0 Array#bsearch behavior

雨燕双飞 提交于 2019-12-18 03:12:43
问题 I noticed that as of Ruby 2.0.0 the array class has a bsearch method that I was testing and I'm not getting the behavior I'd expect. Why does it return a value for 2 and 5 but nil for -1, 1, and 4? arr_in = [-1, 1, 2, 4, 5] arr_in.bsearch { |x| x == 3 } #=> nil arr_in.bsearch { |x| x == -1 } #=> nil arr_in.bsearch { |x| x == 1 } #=> nil arr_in.bsearch { |x| x == 2 } #=> 2 arr_in.bsearch { |x| x == 4 } #=> nil arr_in.bsearch { |x| x == 5 } #=> 5 回答1: arr_in = [-1, 1,2,4,5] arr_in.bsearch{ |x|

Ruby 2.0.0 Array#bsearch behavior

こ雲淡風輕ζ 提交于 2019-12-18 03:12:18
问题 I noticed that as of Ruby 2.0.0 the array class has a bsearch method that I was testing and I'm not getting the behavior I'd expect. Why does it return a value for 2 and 5 but nil for -1, 1, and 4? arr_in = [-1, 1, 2, 4, 5] arr_in.bsearch { |x| x == 3 } #=> nil arr_in.bsearch { |x| x == -1 } #=> nil arr_in.bsearch { |x| x == 1 } #=> nil arr_in.bsearch { |x| x == 2 } #=> 2 arr_in.bsearch { |x| x == 4 } #=> nil arr_in.bsearch { |x| x == 5 } #=> 5 回答1: arr_in = [-1, 1,2,4,5] arr_in.bsearch{ |x|

Determining index from bsearch and lfind?

孤街浪徒 提交于 2019-12-11 08:26:14
问题 I'm trying to get the index of the element in the array after lfind and bsearch return the pointer to the element that it found. I have this so far: (char *) (found - cv->baseAddress); where found is the address of what the functions found, and the base address is the address of element 0. However, the compiler gives me this error: cvector.c:150:28: warning: pointer of type ‘void *’ used in subtraction cvector.c:150:4: warning: return makes integer from pointer without a cast What do I do?

qsort and bsearch an array of pointers

∥☆過路亽.° 提交于 2019-12-11 02:54:28
问题 I need to sort an array of pointers to struc. In fact, I need to do searching among adresses to see if a given pointer to a struct is present in the array. Unfortunately, I don't have nothing "comparable" inside those structures and so I want to sort'em just by address. My code is like that: item* arr[SIZE]; //something is inserted qsort(arr, SIZE, sizeof(item*), (void*)compare_funct); //CUT bsearch(curr, arr, SIZE, sizeof(item*), (void*)compare_funct); I tried creating a compare_funct just

bsearch and struct (custom type)

喜夏-厌秋 提交于 2019-12-10 18:05:06
问题 I have an array like this: typedef struct INSTR { char* str; int argc; } INSTR; const static INSTR instructions[] = { {"blue",1}, {"green",2} }; then I tried to do a bsearch , but I'm getting Segmentation fault message: int comp(const void *a, const void *b) { const INSTR *aa = (INSTR*)a; const INSTR *bb = (INSTR*)b; // if I "return 0;" here i get no error. return strcmp(aa->str, bb->str); } . char *str = get_string(src_buff, size); bsearch(str, instructions, sizeof(instructions) / sizeof

Trouble using bsearch with an array of strings

折月煮酒 提交于 2019-12-04 09:21:57
I am getting some confusing behaviour trying to use the c builtin bsearch on an array of strings in C. Here is the code. I know you can use the builtin strcmp for searching arrays of strings, but I included myStrCmp for debugging purposes because I didn't know why it wasn't working. const char *stateNames[] = {"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi",

Ruby 2.0.0 Array#bsearch behavior

霸气de小男生 提交于 2019-11-30 08:25:33
I noticed that as of Ruby 2.0.0 the array class has a bsearch method that I was testing and I'm not getting the behavior I'd expect. Why does it return a value for 2 and 5 but nil for -1, 1, and 4? arr_in = [-1, 1, 2, 4, 5] arr_in.bsearch { |x| x == 3 } #=> nil arr_in.bsearch { |x| x == -1 } #=> nil arr_in.bsearch { |x| x == 1 } #=> nil arr_in.bsearch { |x| x == 2 } #=> 2 arr_in.bsearch { |x| x == 4 } #=> nil arr_in.bsearch { |x| x == 5 } #=> 5 arr_in = [-1, 1,2,4,5] arr_in.bsearch{ |x| 2 - x } #=> 2 arr_in.bsearch{ |x| -1 - x } #=> -1 arr_in.bsearch{ |x| 3 - x } #=> nil Binary search uses

Using bsearch to find index for inserting new element into sorted array

て烟熏妆下的殇ゞ 提交于 2019-11-29 04:01:47
I have a sorted unique array and want to efficiently insert an element into it that is not in the array like this: a = [1,2,4,5,6] new_elm = 3 insert_at = a.bsearch_index {|x| x > new_elm } # => 2 a.insert(insert_at, new_elm) # now a = [1,2,3,4,5,6] The method bsearch_index doesn't exist: only bsearch , which returns the matching element rather than the matching element's index. Is there any built in way to accomplish this? wvandaal You can use the Enumerator object returned by each_with_index to return a nested array of [value, index] pairs and then conduct your binary search on that array: a