[STL] commonly used traversal search algorithm find, binary_search, adjacent_find, find_if, count, count_if, transform, for_each

Common traversal search algorithm find, binary_search, adjacent_find, find_if, count, count_if, transform, for_each

First, the traversal algorithm

Here Insert Picture Description

Second, the search algorithm

Here Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture DescriptionHere Insert Picture Description

Third, the use case example:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//transform将一个容器的元素搬运到另一个容器中
struct myplus {
	int operator()(int v){
		return v;
	}
};
void myprint(int v) {
	cout << v+100 << " ";
}
void test01() {
	vector<int>v1;
	vector<int>v2;
	for (int i = 0; i < 10; ++i) {
		v1.push_back(i);
	}

	v2.resize(v2.size());//开辟初始化的空间
	//v2.reserve(100);//错误 虽然给了100个空间,但未初始化是不能直接使用的,需要配合push_back();

	transform(v1.begin(), v1.end(), v2.begin(), myplus());//将v1里面的值经过函数对象myplus()放入到容器v2中

	for_each(v2.begin(), v2.end(), myprint);

}

//常用的查找算法

void test02() {

	vector<int>v1;
	for (int i = 0; i < 10; ++i) {
		v1.push_back(i);
	}
	
	vector<int>::iterator ret=find(v1.begin(), v1.end(), 5);
	if (ret != v1.end()) {
		cout << *ret << endl;
	}
	else {
		cout << "未找到" << endl;
	}

}
class Person {
public:
	Person(int id,int age):id(id),age(age){}
	bool operator==(const Person& p)const {
		return p.id == this->id && p.age == this->age;
	}
public:
	int age;
	int id;

};
void test03() {
	vector<Person>v1;
	Person p1(10, 20), p2(20, 30);
	v1.push_back(p1);
	v1.push_back(p2);
	vector<Person>::iterator ret = find(v1.begin(), v1.end(), p1);
	if (ret == v1.end()) {
		cout << "未找到" << endl;
	}
	else {
		cout << "找到" << endl;
	}
}
/////////////////////////////binary_search  二分查找法/////////////////////////////
bool Mysearch2(int val) {
	return val > 5;
}
bool Mysearch(int val) { //函数作用 找到大于5 的值之后返回Ture不然返回false
	return val > 5;
}
void test04() {
	vector<int>v1;
	for (int i = 0; i < 10; i++) {
		v1.push_back(i);
	}

	bool ret=binary_search(v1.begin(), v1.end(), 5);
	if (ret) {
		cout << "存在" << endl;
	}
	else {
		cout << "不存在" << endl;
	}
	/////////////////////adjacent_find///////////////////////////
	vector<int>::iterator it=adjacent_find(v1.begin(), v1.end());
	if (it != v1.end()) {
		cout << "找到相邻重复元素" <<*it<< endl;
	}
	else {
		cout << "没有找到相邻重复元素" << endl;
	}
	////////////////////////////////////////find_if条件查找//////////////////////////////////// 
	//find_if 会根据我们的条件(函数),返回第一个满足条件的元素的迭代器
	it=find_if(v1.begin(), v1.end(), Mysearch);
	if (it != v1.end()) {
		cout << "找到相邻重复元素" << *it << endl;
	}
	else {
		cout << "没有找到相邻重复元素" << endl;
	}
	/////////////////////////////////////////////////count count_if////////////////////////////////////
	//count count_if
	int num=count(v1.begin(), v1.end(), 9);//查找该区间内9出现的次数
	cout << "9出现的个数" << num << endl;

	int num = count_if(v1.begin(), v1.end(), Mysearch2);//统计元素中大于5的个数,条件函数是Mysearch02()中。
}
int main(void) {
	//test01();
	//test02();
	//test03();
	test04();
	return 0;
 } 
Published 57 original articles · won praise 28 · views 4121

Guess you like

Origin blog.csdn.net/weixin_41747893/article/details/102960659