01 常用查找算法_find

03 常用查找算法_adjacent_find

04 常用查找算法 binary_search

05 常用的查找算法_count

06 常用的查找算法_count_if


01 常用查找算法_find

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>


void test01()
{
	vector <int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector <int>::iterator it = find(v.begin(), v.end(), 5);
	if (it != v.end())
	{
		cout << "找到" << endl;
	}
	else
	{
		cout << "没找到" << endl;

	}
}

class Person {
public:
	Person(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	//重载== find知道如何对比Person数据类型
	bool operator== (const Person& p)
	{
		if (this->m_name == p.m_name && this->m_age == p.m_age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	string m_name;
	int m_age;
};

void test02()
{
	vector <Person> v;
	Person p ("张三", 12);
	Person p2("李四", 54);
	Person p3("王二", 34);
	Person p4("赵六", 45);
	v.push_back(p);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	vector <Person>::iterator it = find(v.begin(), v.end(), p2);
	if (it != v.end())
	{
		cout << "找到" << endl;
		cout << "姓名" << it->m_name << " 年龄" << it->m_age << endl;
	}
	else
	{
		cout << "没找到" << endl;
		
	}
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

03 常用查找算法_adjacent_find

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>

//找重复元素
void test01()
{
	vector<int> v;
	v.push_back(10);
	v.push_back(30);
	v.push_back(34);
	v.push_back(54);
	v.push_back(54);
	v.push_back(45);
	vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
	if (pos != v.end())
	{
		cout << "找到重复元素" << endl;
		cout << *pos << endl;
	}
	else
	{
		cout << "没找到重复元素" << endl;
	}

}

int main()
{
	test01();

	system("pause");

	return 0;
}

04 常用查找算法 binary_search

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>

//二分找元素
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//v.push_back(2); //如果是无序序列,结果未知
	//查找容器中是否有9元素
	//注意:容器必须是有序的序列
	int ret = binary_search(v.begin(), v.end(), 9);
	if (ret)
	{
		cout << "找到" << endl;
	}
	else
	{
		cout << "没找到" << endl;
	}

	
}

int main()
{
	test01();

	system("pause");

	return 0;
}


//总结:二分查找效率很高,值得注意的是查找容器元素必须是有序序列

05 常用的查找算法_count

#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>


void test01()
{
	vector<int> v;
	v.push_back(30);
	v.push_back(20);
	v.push_back(50);
	v.push_back(20);
	v.push_back(20);
	v.push_back(20);
	int num = count(v.begin(), v.end(), 20);
	cout << "20的个数是:" << num << endl;

}

class Person {

public:
	Person(string name , int age) {
		this->m_name = name;
		this->m_age = age;
	}
	bool operator==(const Person &p)
	{
		if (this->m_age == p.m_age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	string m_name;
	int m_age;
};

void test02()
{
	vector<Person> v;
	Person p1("关羽", 45);
	Person p2("张飞", 45);
	Person p3("刘备", 45);
	Person p4("曹操", 67);
	Person p5("赵云", 87);
	Person p("诸葛亮", 45);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	int num = count(v.begin(), v.end(), p);
	cout << "和诸葛亮同龄个数:" << num << endl;
}

int main()
{
	test01();
	test02();
	system("pause");
	return 0;

}

//总结:统计自定义数据类型时,需要配合重载operator==

06 常用的查找算法_count_if

#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>


class Greate20 {
public:
	bool operator()(int val)
	{
		return val >20;
	}
};

void test01()
{
	vector<int> v;
	v.push_back(30);
	v.push_back(20);
	v.push_back(50);
	v.push_back(20);
	v.push_back(20);
	v.push_back(20);
	int num = count_if(v.begin(), v.end(), Greate20());
	
	cout << "大于20的个数" << num << endl;
}

class Person {

public:
	Person(string name, int age) {
		this->m_name = name;
		this->m_age = age;
	}
	
	string m_name;
	int m_age;
};

class AGE245 {
public:
	bool operator()(const Person &p)
	{
		return p.m_age > 45;
	}
};

void test02()
{
	vector<Person> v;
	Person p1("关羽", 45);
	Person p2("张飞", 45);
	Person p3("刘备", 45);
	Person p4("曹操", 67);
	Person p5("赵云", 87);
	Person p("诸葛亮", 45);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	int num = count_if(v.begin(), v.end(), AGE245());

	cout << "大于45岁的人数" << num << endl;
}



int main()
{
	test01();
	test02();
	system("pause");
	return 0;

}