[c++学习笔记28]:常用查找算法

今天学习STL常用查找算法。

学习目标:

  • 掌握常用的查找算法

算法简介:

  • find //查找元素
  • find_if //按条件查找元素
  • adjacent_find //查找相邻重复元素
  • binary_search //二分查找法
  • count //统计元素个数
  • count_if //按条件统计元素个数
1 find

功能描述:

  • 查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()

函数原型:

  • find(iterator beg, iterator end, value);

    // 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置

    // beg 开始迭代器

    // end 结束迭代器

    // value 查找的元素

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

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

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

};
void test02()
{
	Person p1("aa", 10);
	Person p2("bb", 20);
	Person p3("cc", 30);
	Person p4("dd", 40);
	vector<Person>v2;
	v2.push_back(p1);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);
	vector<Person>::iterator ss = find(v2.begin(), v2.end(), p2);
	if (ss == v2.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了!!!" << "  年龄:  " << ss->m_Age << "姓名: " << ss->m_name << endl;
	}
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

总结: 利用find可以在容器中找指定的元素,返回值是迭代器

5.2.2 find_if

功能描述:

  • 按条件查找元素

函数原型:

  • find_if(iterator beg, iterator end, _Pred);

    // 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置

    // beg 开始迭代器

    // end 结束迭代器

    // _Pred 函数或者谓词(返回bool类型的仿函数)

#include<iostream>
using namespace std;
#include<string>
#include <vector>
#include <algorithm>
class Greaterfive 
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
void test01() {
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//查找有没有5这个元素
	vector<int>::iterator it = find_if(v.begin(), v.end(), Greaterfive());
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了!!!" << *it<<endl;
	}

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

};
class Greater20 {
public:
	bool operator()(const Person& p)
	{
		return p.m_Age > 20;
	}
};
void test02()
{
	Person p1("aa", 10);
	Person p2("bb", 20);
	Person p3("cc", 30);
	Person p4("dd", 40);
	vector<Person>v2;
	v2.push_back(p1);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);
	vector<Person>::iterator ss = find_if(v2.begin(), v2.end(), Greater20());
	if (ss == v2.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了!!!" << "  年龄:  " << ss->m_Age << "姓名: " << ss->m_name << endl;
	}
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

总结:find_if按条件查找使查找更加灵活,提供的仿函数可以改变不同的策略

3 adjacent_find

功能描述:

  • 查找相邻重复元素

函数原型:

  • adjacent_find(iterator beg, iterator end);

    // 查找相邻重复元素,返回相邻元素的第一个位置的迭代器

    // beg 开始迭代器

    // end 结束迭代器

#include <algorithm>
#include <vector>

void test01()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(5);
	v.push_back(2);
	v.push_back(4);
	v.push_back(4);
	v.push_back(3);

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

总结:面试题中如果出现查找相邻重复元素,记得用STL中的adjacent_find算法

5.2.4 binary_search

功能描述:

  • 查找指定元素是否存在

函数原型:

  • bool binary_search(iterator beg, iterator end, value);

    // 查找指定的元素,查到 返回true 否则false

    // 注意: 在无序序列中不可用

    // beg 开始迭代器

    // end 结束迭代器

    // value 查找的元素

#include <algorithm>
#include <vector>

void test01()
{
	vector<int>v;

	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//二分查找
	bool ret = binary_search(v.begin(), v.end(),2);
	if (ret)
	{
		cout << "找到了" << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}
}

int main() {

	test01();

	system("pause");

	return 0;
}

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

5 count

功能描述:

  • 统计元素个数

函数原型:

  • count(iterator beg, iterator end, value);

    // 统计元素出现次数

    // beg 开始迭代器

    // end 结束迭代器

    // value 统计的元素

#include<iostream>
using namespace std;
#include<string>
#include <vector>
#include <algorithm>
void test01() {
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	v.push_back(2);
	v.push_back(8);
	v.push_back(4);
	v.push_back(2);
	v.push_back(2);
	v.push_back(8);
	v.push_back(4);
	v.push_back(2);
	int num = count(v.begin(), v.end(), 2);
	cout << "2的个数为: " << num << endl;

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

};
class Greater20 {
public:
	bool operator()(const Person& p)
	{
		return p.m_Age > 20;
	}
};
void test02()
{
	Person p1("刘备", 35);
	Person p2("关羽", 35);
	Person p3("张飞", 35);
	Person p4("赵云", 30);
	Person p5("曹操", 25);
	vector<Person>v2;
	v2.push_back(p1);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);
	Person pp("诸葛亮", 35);
	int num = count(v2.begin(), v2.end(), pp);
	cout << "和诸葛亮同岁的num = " << num << endl;
}

int main()
{
	test01();
	test02();
	//test03();
	system("pause");
	return 0;
}
6 count_if

功能描述:

  • 按条件统计元素个数

函数原型:

  • count_if(iterator beg, iterator end, _Pred);

    // 按条件统计元素出现次数

    // beg 开始迭代器

    // end 结束迭代器

    // _Pred 谓词

#include<iostream>
using namespace std;
#include<string>
#include <vector>
#include <algorithm>
class Greatfour {
public:
	bool operator()(int ral)
	{
		return ral > 4;
	}
};
void test01() {
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	v.push_back(2);
	v.push_back(8);
	v.push_back(4);
	v.push_back(2);
	v.push_back(2);
	v.push_back(8);
	v.push_back(4);
	v.push_back(2);
	int num = count_if(v.begin(), v.end(), Greatfour());
	cout << "大于4的个数为: " << num << endl;

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

	int m_Age;
	string m_name;

};
class lessAge {
public:
	bool operator()(const Person& p)
	{
		return p.m_Age < 35;
	}
		
};


void test02()
{
	Person p1("刘备", 35);
	Person p2("关羽", 35);
	Person p3("张飞", 35);
	Person p4("赵云", 30);
	Person p5("曹操", 25);
	vector<Person>v2;
	v2.push_back(p1);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);
	v2.push_back(p5);
	Person pp("诸葛亮", 35);
	int num = count_if(v2.begin(), v2.end(), lessAge());
	cout << "小于35岁的num = " << num << endl;
}

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

**总结:**按值统计用count,按条件统计用count_if

啊 可算敲完了。
最近准备去公司实习,但感觉自己什么都不会 慌慌慌!!!
加油加油!!!

打个小广告,欢迎关注我的公众号“麦香E站”,分享人工智能,信通学习,数学提高,外语学习,生活理财等多方面知识以及我多年积累的学习资源分享~
公众号后台回复 “编程” 获取我多年来积攒的海量编程电子书~
后台回复“latex”获取我倾力所作latex快速入门手册~
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值