bsearch

Is the specification of `bsearch` in C++11 & C++14 defective?

谁说我不能喝 提交于 2019-11-28 09:35:14
Following on from my answer to this question , in both C++11 and C++14: [C++11, C++14: 25.5/2]: The contents are the same as the Standard C library header <stdlib.h> with the following exceptions: [C++11, C++14: 25.5/3]: The function signature: bsearch(const void *, const void *, size_t, size_t, int (*)(const void *, const void *)); is replaced by the two declarations: extern "C" void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); extern "C++" void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (

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

被刻印的时光 ゝ 提交于 2019-11-27 22:16:13
问题 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? 回答1: You can use the Enumerator object returned by each_with_index to

Is the specification of `bsearch` in C++11 & C++14 defective?

南楼画角 提交于 2019-11-27 03:01:31
问题 Following on from my answer to this question, in both C++11 and C++14: [C++11, C++14: 25.5/2]: The contents are the same as the Standard C library header <stdlib.h> with the following exceptions: [C++11, C++14: 25.5/3]: The function signature: bsearch(const void *, const void *, size_t, size_t, int (*)(const void *, const void *)); is replaced by the two declarations: extern "C" void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void