STL series (two) binary search

STL series (two) binary search

Function: binary_search

  • If there is a function reference not mentioned in the unknown article in the content, please check the previous article STL series (1) sort usage
  • There will be a lot of similar but different words in the content of this issue. Read carefully, pay attention to comparison, deepen memory, and don't feel that the content is repetitive and upset
  • Note the bold statement
  • Leave a message or comment if you have any questions

Usage one (basic search)

content:

        binary_search( 数组名 + n1, 数组名 + n2,)
    
        n1和n2都是 int 类型的表达式 , 可以包含变量

        如果n1 = 0,+ n1 可以不写

        查找区间为下标范围为[n1,n2)的元素, 下标为n2的元素不在查找区间内 
                   
        在该区间内查找"等于"值的元素, 返回值为true(找到)false(没找到)
          
        "等于"的含义: a 等于 b <==> a < b和b < a都不成立

example:

	int a[] = {
    
     12,45,3,98,21,7 };
	sort(a, a + 6);
	Print(a, 6);       //结果: 3,7,12,21,45,98
	cout << "Result : " << binary_search(a, a + 6, 12) << endl;  //Result : 1
	cout << "Result : " << binary_search(a, a + 6, 77) << endl;  //Result : 0
  • Use sort before using STL binary search;

Usage two (custom sorting rule search)

content:

In order to sort by custom sorting rules, the elements are arbitrary T-type arrays for binary search

binary_search(数组名 +n1 , 数组名 + n2 ,, 排序规则结果名() );
n1和n2都是int类型得表达式,可以包含变量

如果 n1 = 0 ,+ n1可以不写

查找区间为下标范围[n1,n2)的元素 , 下标为n2的元素不在查找区间内

在该区间内查找"等于"值的元素, 返回值为true(找到)false(没找到)

*查找时的排序规则,必须和排序时的规则一致!

"等于"的含义:  a 等于 b <==> "a必须在b前面""b必须在a前面"都不成立

Important things have been emphasized many times to deepen the impression! Not a water article!

example:

    int a[] = {
    
     12,45,3,98,21,7 };
	sort(a, a + 6, Rule1());   //按从小到大排序(此处使用Rule1规则进行排序)
	Print(a, 6);               // 21,12,3,45,7,98
	cout << "Result : " << binary_search(a, a + 6, 7) << endl;          //Result : 0
	cout << "Result : " << binary_search(a, a + 6, 8, Rule1()) << endl; //Result : 1
	return 0;

The fourth line of the above code is the wrong code

Here the compiler return value of 0 does not mean that it was not found!

binary_search () binary search rules must be consistent with the collation , otherwise the return value does not make sense

Complete code: (including custom functions)

#include<iostream>
#include<algorithm>
#include<cstring>

using namespace std;
struct Rule1 {
    
    
	bool operator()(const int& a1, const int& a2)const {
    
    
		return a1 % 10 < a2 % 10;
	}
};

void Print(int a[], int size);

int main() {
    
    
	int a[] = {
    
     12,45,3,98,21,7 };
	sort(a, a + 6);
	Print(a, 6);
	cout << "Result : " << binary_search(a, a + 6, 12) << endl;
	cout << "Result : " << binary_search(a, a + 6, 77) << endl;
	sort(a, a + 6, Rule1());//按从小到大排序
	Print(a, 6);
	cout << "Result : " << binary_search(a, a + 6, 7) << endl;
	cout << "Result : " << binary_search(a, a + 6, 8, Rule1()) << endl;
	return 0;
}

void Print(int a[], int size) {
    
    
	int i;
	for (i = 0; i < size - 1; i++) {
    
    
		cout << a[i] << ",";
	}
	cout << a[i];
	cout << endl;
}

Function lower_bound

Usage one (find the lower bound)

Search in the basic data types sorted from small to large with element type T

T * lower_bound(数组名 + n1 , 数组名 + n2 ,);

Return a pointer T * p;

*p is the element with the smallest subscript in the search interval, which is greater than or equal to "value". If not found, p points to the element with subscript n2

Note: n2 is not in the query range!

example:

	int a[NUM] = {
    
     12,5,3,5,98,21,7 };
	sort(a, a + NUM);
	Print(a, NUM);  // 结果: 3,5,5,7,12,21,98
	int* p1 = lower_bound(a, a + NUM, 5); //范围整个数组,p1指向下标最小的 大于等于5的元素
	cout << " p1指向的内容:"<< *p1 << "  下标:" << p1 - a << endl;   // 结果:5,1

Usage two (custom rules to find the lower bound)

Search in an array whose element type is T and sorted according to a custom sorting rule

T * lower_bound(数组名 + n1 , 数组名 + n2 ,, 排序规则结构名());

Return a pointer T * p;

*p is the smallest subscript in the search interval. According to a custom sorting rule, you can sort the element after the "value". If you can't find it, p points to the element with the subscript n2

Note: n2 is not in the query range!

example:

	int a[NUM] = {
    
     12,5,3,5,98,21,7 };
    sort(a, a + NUM, Rule1());
	Print(a, NUM); // 结果 :21,12,3,5,5,7,98
	cout << *lower_bound(a, a + NUM, 16, Rule1()) << endl;    // 结果 : 7 (输出元素值)
	cout << lower_bound(a, a + NUM, 25, Rule1()) - a << endl; // 结果 : 3 (输出下标值)

You can try to analyze the reason for the following content


Function upper_bound

Usage one (find upper bound)

content:

Search in an array of basic types sorted from small to large with element type T

T * upper_bound(数组名 + n1 , 数组名 + n2 , 值);

Return a pointer T * p;

*p is the element with the smallest subscript in the search interval and greater than "value". If it is not found, p points to the element with subscript n2

example:

	int a[NUM] = {
    
     12,5,3,5,98,21,7 };
	sort(a, a + NUM);
    Print(a, NUM);  // 结果: 3,5,5,7,12,21,98
    int* p2 = upper_bound(a, a + NUM, 5); //范围整个数组,p2指向下标最小的 大于5的元素
	cout << *p2 << endl;  // 结果:7
	cout << *upper_bound(a, a + NUM, 13) << endl;//查找大于13的下标最小的元素值 结果 :21

Usage two (custom rule to find upper bound)

content:

Search in an array whose elements are of arbitrary T type and sorted according to a custom sorting rule

T * upper_bound(数组名 + n1 , 数组名 + n2 , 值 , 排序规则结构体());

Return a pointer T * p;

*p is the element with the smallest subscript in the search interval. According to custom sorting rules, **must** be the element after "value". If it cannot be found, p points to the element with subscript n2

example:

	int a[NUM] = {
    
     12,5,3,5,98,21,7 };
    sort(a, a + NUM, Rule1());
	Print(a, NUM); // 结果 :21,12,3,5,5,7,98
	cout << *lower_bound(a, a + NUM, 16, Rule1()) << endl;    // 结果 : 7 (输出元素值)
	cout << lower_bound(a, a + NUM, 25, Rule1()) - a << endl; // 结果 : 3 (输出下标值)
	cout << upper_bound(a, a + NUM, 18, Rule1()) - a << endl; // 结果 : 7 (无意义)(个位数大于8无法找到)(找不到时返回终点元素)
	if (upper_bound(a, a + NUM, 18, Rule1()) == a + NUM)
	cout << "not found" << endl;     // ==>not found;
	cout << *upper_bound(a, a + NUM, 5, Rule1()) << endl;      // 结果 : 7 (自己想想原因)
	cout << *upper_bound(a, a + NUM, 4, Rule1()) << endl;      // 结果 : 5

Complete code:

#include<iostream>
#include<algorithm>
#include<cstring>

#define NUM 7
using namespace std;
struct Rule1 {
    
    
	bool operator()(const int& a1, const int& a2)const {
    
    
		return a1 % 10 < a2 % 10;
	}
};
void Print(int a[], int size);

int main() {
    
    
	int a[NUM] = {
    
     12,5,3,5,98,21,7 };
	sort(a, a + NUM);
	Print(a, NUM);  // 结果: 3,5,5,7,12,21,98
	int* p1 = lower_bound(a, a + NUM, 5); //范围整个数组,p1指向下标最小的 大于等于5的元素
	cout << " p1指向的内容:"<< *p1 << "  下标:" << p1 - a << endl;   // 结果:5,1
	int* p2 = upper_bound(a, a + NUM, 5); //范围整个数组,p2指向下标最小的 大于5的元素
	cout << *p2 << endl;  // 结果:7
	cout << *upper_bound(a, a + NUM, 13) << endl;//查找大于13的下标最小的元素值 结果 :21
	sort(a, a + NUM, Rule1());
	Print(a, NUM); // 结果 :21,12,3,5,5,7,98
	cout << *lower_bound(a, a + NUM, 16, Rule1()) << endl;    // 结果 : 7 (输出元素值)
	cout << lower_bound(a, a + NUM, 25, Rule1()) - a << endl; // 结果 : 3 (输出下标值)
	cout << upper_bound(a, a + NUM, 18, Rule1()) - a << endl; // 结果 : 7 (无意义)(个位数大于8无法找到)(找不到时返回终点元素)
	if (upper_bound(a, a + NUM, 18, Rule1()) == a + NUM)
		cout << "not found" << endl;     // ==>not found;
	cout << *upper_bound(a, a + NUM, 5, Rule1()) << endl;      // 结果 : 7 (自己想想原因)
	cout << *upper_bound(a, a + NUM, 4, Rule1()) << endl;      // 结果 : 5
	return 0;
}

void Print(int a[], int size) {
    
    
	int i;
	for (i = 0; i < size - 1; i++) {
    
    
		cout << a[i] << ",";
	}
	cout << a[i];
	cout << endl;
}

Guess you like

Origin blog.csdn.net/zhuiyizhifengfu/article/details/113573619