常用查找算法

1.1	find 按值查找  
1.2	find_if 按条件进行查找
1.3	adjacent_find算法 查找相邻重复元素
1.4	binary_search算法 二分查找法
1.4.1	有序序列
1.5	count 按值统计
1.6	count_if 按条件统计

find

void test01() {
	vector<int>v;

	for (int i = 0; i < 10; ++i) {
		v.push_back(rand() % 100);
	}

	for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
	cout << endl;

	vector<int>::iterator pos = find(v.begin(), v.end(), 5);
	if (pos != v.end()) {
		cout << "找到了,值为" << *pos << endl;
	}
	else {
		cout << "未找到" << endl;
	}
}

在这里插入图片描述

//利用find查找自定义类型数据
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&&this->m_name == p.m_name) {
			return true;
		}
		return false;
	}
	string m_name;
	int m_age;
};

void test02() {
	vector<Person>v;

	Person p1("渣渣猫", 26);
	Person p2("土拨鼠", 25);
	Person p3("波波", 19);
	Person p4("锯嘴葫芦", 18);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	vector<Person>::iterator pos = find(v.begin(), v.end(), p2);
	if (pos != v.end()) {
		cout << "找到了,姓名为:" << (*pos).m_name << " 年龄是:" << pos->m_age << endl;
	}
	else {
		cout << "未找到" << endl;
	}
}

在这里插入图片描述

find_if

class MyCompar :public binary_function<Person*,Person*,bool>{
public:
	bool operator()(Person *p1, Person *p2) const{
		if (p1->m_name == p2->m_name&&p1->m_age == p2->m_age) {
			return true;
		}
		return false;
	}
};
void test03() {
	vector<Person*>v;

	Person p1("渣渣猫", 26);
	Person p2("土拨鼠", 25);
	Person p3("波波", 19);
	Person p4("锯嘴葫芦", 18);

	v.push_back(&p1);
	v.push_back(&p2);
	v.push_back(&p3);
	v.push_back(&p4);

	Person *p = new Person("Nick", 30);
	vector<Person*>::iterator pos = find_if(v.begin(), v.end(), bind2nd(MyCompar(),p));
	if (pos != v.end()) {
		cout << "找到了,姓名为:" << (*pos)->m_name << " 年龄是:" << (*pos)->m_age << endl;
	}
	else {
		cout << "未找到" << endl;
	}
}

在这里插入图片描述

adjacent_find

void test04() {
	vector<int>v;

	v.push_back(5);
	v.push_back(15);
	v.push_back(25);
	v.push_back(25);
	v.push_back(25);
	v.push_back(35);
	v.push_back(35);
	v.push_back(55);
	v.push_back(5);

	vector<int>::iterator pos = adjacent_find(v.begin(), v.end());
	if (pos != v.end()) {
		cout << "相邻的重复元素为" << *pos << endl;
	}
	else {
		cout << "未找到" << endl;
	}
}

在这里插入图片描述

binary_search

void test05() {
	vector<int>v;

	for (int i = 0; i < 10; ++i) {
		v.push_back(i);
	}

	bool ret = binary_search(v.begin(), v.end(), 4);
	if (ret) {
		cout << "找到了4" << endl;
	}
	else {
		cout << "未找到" << endl;
	}
}

在这里插入图片描述

count

count_if

class LargeThanFour {
public:
	bool operator()(int val) {
		return val > 4;
	}
};

void test06() {
	vector<int>v;

	for (int i = 0; i < 10; ++i) {
		v.push_back(i);
	}
	v.push_back(4);
	v.push_back(4);
	v.push_back(4);

	int num = count(v.begin(), v.end(), 4);
	cout << "4的个数为:" << num << endl;

	int num1 = count_if(v.begin(), v.end(), LargeThanFour());
	cout << "大于4的个数为:" << num1 << endl;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值