C++:STL-常用查找算法

在这里插入图片描述

#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>

using namespace std;

//find 查找元素,内置类型
void test01()
{
	vector<int>v = { 1,2,3,4,5,6,7,8,9 };

	//查找在区间内出现的第一个5,返回当前查找元素迭代器
	vector<int>::iterator it = find(v.begin(), v.end(), 5);
	if (it != v.end())
	{
		cout << "找到!" << endl;
	}
	else
	{
		cout << "未找到!" << endl;
	}
}
//find 查找元素,自定义类型,得告诉编译器如何做对比
class Person
{
public:
	Person(string name, int age)
	{
		m_name = name;
		m_age = age;
	}
	bool operator==(const Person &p)
	{
		if (this->m_age == p.m_age&&this->m_name == p.m_name)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	string m_name;
	int m_age;
};
void test02()
{
	vector<Person>v;
	//创建数据
	Person p1("王大锤", 18);
	Person p2("喵喵喵", 20);
	Person p3("嘤嘤嘤", 19);
	//插入数据
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);

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

}

//find_if 按条件查找-----------------------------------------------
//find_if 查找元素,内置类型
class GreaterFive
{
public:
	bool operator()(int val)
	{
		return val>5;
	}
};
void test03()
{
	vector<int>v = { 1,2,3,4,5,6,7,8,9 };

	//查找在区间内出现的第一个5,返回当前查找元素迭代器
	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());
	if (it != v.end())
	{
		cout << "找到!" << endl;
		for (; it != v.end(); it++)
		{
			cout << *it << " ";
		}
		cout << endl;
	}
	else
	{
		cout << "未找到!" << endl;
	}
}
//find_if 自定义数据类型
//一元谓词
class GreaterAge
{
public:
	bool operator()(Person &p)
	{
		return p.m_age > 19;
	}
};
void test04()
{
	vector<Person>v;
	//创建数据
	Person p1("王大锤", 18);
	Person p2("喵喵喵", 20);
	Person p3("嘤嘤嘤", 19);
	//插入数据
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);

	vector<Person>::iterator it = find_if(v.begin(), v.end(),GreaterAge());
	if (it != v.end())
	{
		cout << "找到年龄大于19的人:姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
	}
	else
	{
		cout << "未找到!" << endl;
	}
}

//adjacent_find  查找相邻重复元素
void test05()
{
	vector<int>v = { 0,1,1,2,3,3,4,4,5,5,6,7,8,8 ,1};
	
	int count = 0;
	while (1)
	{
		vector<int>::iterator it = adjacent_find(v.begin(), v.end());

		if (it != v.end())
		{
			v.erase(it);
			count++;
		}
		else
		{
			break;
		}
	}
	cout << "已删除:" << count << "个相邻的重复元素" << endl;
	for (auto i : v)
	{
		cout << i << " ";
	}
	cout << endl;

}

//binary_search  查找指定元素是否存在,存在返回true,不存在返回false
//在无序序列中不可用,二分查找法
void test06()
{
	vector<int>v = { 0,1,2,3,4,5,6,7,8,9 };

	if (binary_search(v.begin(), v.end(), 6))
	{
		cout << "元素6存在" << endl;
	}
	else
	{
		cout << "该元素不存在" << endl;
	}

}

//count 统计元素个数
void test07()
{
	vector<int>v = { 0,1,2,2,2,2,3,4,5,6,7,8,9 };
	int num1 = count(v.begin(), v.end(), 2);
	cout << "内置类型的个数为:" << num1 << endl;

	//需重载==运算符
	vector<Person>p;
	Person p1("王大锤", 18);
	Person p4("王大锤", 18);
	Person p2("喵喵喵", 20);
	Person p3("嘤嘤嘤", 19);
	p.push_back(p1);
	p.push_back(p2);
	p.push_back(p3);
	p.push_back(p4);

	Person p5("王大锤", 18);

	int num2 = count(p.begin(), p.end(), p5);
	cout << "自定数据类型:" << num2 << endl;
}
//count_if 按条件统计元素个数
class GreatTwo
{
public:
	bool operator()(int val)
	{
		return val > 2;
	}
};
void test08()
{
	vector<int>v = { 0,1,2,3,4,5,6,7,8,9 };

	int num = count_if(v.begin(), v.end(),GreatTwo());
	cout << "大于2的数有:" << num << "个" << endl;

	//需调用一元谓词
	vector<Person>p;
	Person p1("王大锤", 18);
	Person p2("喵喵喵", 20);
	Person p3("嘤嘤嘤", 19);
	Person p4("王小锤", 21);
	p.push_back(p1);
	p.push_back(p2);
	p.push_back(p3);
	p.push_back(p4);


	int num2 = count_if(p.begin(), p.end(), GreaterAge());
	cout << "自定数据类型:" << num2 << endl;
}


int main()
{
	test08();

	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值