Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::bsearch

      From cppreference.com
      <cpp‎ |algorithm
       
       
      Algorithm library
      Constrained algorithms and algorithms on ranges(C++20)
      Constrained algorithms, e.g.ranges::copy,ranges::sort, ...
      Execution policies(C++17)
      Sorting and related operations
      Partitioning operations
      Sorting operations
      Binary search operations
      (on partitioned ranges)
      Set operations (on sorted ranges)
      Merge operations (on sorted ranges)
      Heap operations
      Minimum/maximum operations
      (C++11)
      (C++17)
      Lexicographical comparison operations
      Permutation operations
      C library
      bsearch
      Numeric operations
      Operations on uninitialized memory
       
      Defined in header<cstdlib>
      void* bsearch(constvoid* key,constvoid* ptr,std::size_t count,

                     std::size_t size,/* c-compare-pred */* comp);
      void* bsearch(constvoid* key,constvoid* ptr,std::size_t count,

                     std::size_t size,/* compare-pred */* comp);
      (1)
      extern"C"using/* c-compare-pred */=int(constvoid*,constvoid*);
      extern"C++"using/* compare-pred */=int(constvoid*,constvoid*);
      (2)(exposition only*)

      Finds an element equal to element pointed to bykey in an array pointed to byptr. The array containscount elements ofsize bytes each and must be partitioned with respect to the object pointed to bykey, that is, all the elements that compare less than must appear before all the elements that compare equal to, and those must appear before all the elements that compare greater than the key object. A fully sorted array satisfies these requirements. The elements are compared using function pointed to bycomp.

      The behavior is undefined if the array is not already partitioned in ascending order with respect to key, according to the same criterion thatcomp uses.

      If the array contains several elements thatcomp would indicate as equal to the element searched for, then it is unspecified which element the function will return as the result.

      Contents

      [edit]Parameters

      key - pointer to the element to search for
      ptr - pointer to the array to examine
      count - number of element in the array
      size - size of each element in the array in bytes
      comp - comparison function which returns ​a negative integer value if the first argument isless than the second, a positive integer value if the first argument isgreater than the second and zero if the arguments are equivalent.key is passed as the first argument, an element from the array as the second.

      The signature of the comparison function should be equivalent to the following:

       int cmp(constvoid*a,constvoid*b);

      The function must not modify the objects passed to it and must return consistent results when called for the same objects, regardless of their positions in the array.

      [edit]Return value

      Pointer to the found element or null pointer if the element has not been found.

      [edit]Notes

      Despite the name, neither C nor POSIX standards require this function to be implemented using binary search or make any complexity guarantees.

      The two overloads provided by the C++ standard library are distinct because the types of the parametercomp are distinct (language linkage is part of its type).

      [edit]Example

      Run this code
      #include <array>#include <cstdlib>#include <iostream> template<typename T>int compare(constvoid*a,constvoid*b){constauto&arg1=*(static_cast<const T*>(a));constauto&arg2=*(static_cast<const T*>(b));constauto cmp= arg1<=> arg2;return cmp<0?-1:  cmp>0?+1:0;} int main(){std::array arr{1,2,3,4,5,6,7,8}; for(constint key:{4,8,9}){constint* p=static_cast<int*>(            std::bsearch(&key,                arr.data(),                arr.size(),                sizeof(decltype(arr)::value_type),                compare<int>)); std::cout<<"value "<< key;if(p)std::cout<<" found at position "<<(p- arr.data())<<'\n';elsestd::cout<<" not found\n";}}

      Output:

      value 4 found at position 3value 8 found at position 7value 9 not found

      [edit]See also

      sorts a range of elements with unspecified type
      (function)[edit]
      returns range of elements matching a specific key
      (function template)[edit]
      C documentation forbsearch
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/algorithm/bsearch&oldid=173210"

      [8]ページ先頭

      ©2009-2025 Movatter.jp