STL常用算法——常用查找算法

自定义类型都要用仿函数判断

1.find()

cpp 复制代码
class Person
{
public:
	Person(string name,int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	bool operator==(const Person &p)//重载operator=
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		return false;
	}
	string m_Name;
	int m_Age;
};
void test01()//find查找内置类型
{
	vector<int> v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);
	//find查找内置类型
	auto it = find(v1.begin(), v1.end(), 40);//auto自动推出it类型(vector<int>::iterator)
	if (it == v1.end())
	{
		cout << "没找到这个元素" << endl;
	}
	else
	{
		cout << "找到这个元素:" << *it << endl;
	}
}
void test02()find查找自定义类型//类内部要重载operator=
{
	vector<Person> p;
	Person p1("张三", 10);
	Person p2("李四", 15);
	Person p3("王五", 13);
	Person p4("赵六", 16);
	p.push_back(p1);
	p.push_back(p2);
	p.push_back(p3);
	p.push_back(p4);
	Person pf("赵六", 14);
	auto it = find(p.begin(), p.end(), pf);//auto自动推出it类型(vector<int>::iterator)
	if (it == p.end())
	{
		cout << "没找到这个人" << endl;
	}
	else
	{
		cout << "找到了,姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
	}
}

2.find_if()

cpp 复制代码
class Person
{
public:
	Person(string name,int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};
class Greater20//test01
{
public:
	bool operator()(int val)
	{
		return val > 20;
	}
 };
class MyCompare//test02
{
public:
	bool operator()(Person& p)
	{
		return (p.m_Age > 15);
	}
};
void test01()//find_if查找内置类型
{
	vector<int> v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(50);
	vector<int>::iterator it=find_if(v1.begin(), v1.end(), Greater20());
	cout << "找到了:" << *it << endl;
}
void test02()find_if查找自定义类型
{
	vector<Person> p;
	Person p1("张三", 10);
	Person p2("李四", 15);
	Person p3("王五", 13);
	Person p4("赵六", 16);
	p.push_back(p1);
	p.push_back(p2);
	p.push_back(p3);
	p.push_back(p4);
	Person pf("赵六", 16);
	vector<Person>::iterator it = find_if(p.begin(), p.end(),MyCompare());
	if (it == p.end())
	{
		cout << "没找到这个人" << endl;
	}
	else
	{
		cout << "找到了,姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
	}
}

3.adjacent_find()

cpp 复制代码
void test01()
{
	vector<int> v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(40);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(40);
	v1.push_back(50);
	vector<int>::iterator it = adjacent_find(v1.begin(), v1.end());//查找相邻相同元素
	cout << "找到了:" << *it << endl;
}

4.binary_search()//二分查找

cpp 复制代码
void test01()
{
	vector<int> v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(40);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(40);
	v1.push_back(50);
	sort(v1.begin(), v1.end());//降序不行
	bool ret = binary_search(v1.begin(), v1.end(), 30);
	if (ret)
	{
		cout << "找到了" << endl;
	}
	else
	{
		cout << "没找到" << endl;
	}
}

5.count()

cpp 复制代码
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;
		}
		return false;
	}
	string m_Name;
	int m_Age;
};
void test01()//count查找内置类型
{
	vector<int> v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(40);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(40);
	v1.push_back(50);
	int num = count(v1.begin(), v1.end(), 40);//统计40的个数
	cout << "40的个数是:" << num << endl;

}
void test02()//count查找自定义类型
{
	vector<Person> p;
	Person p1("张三", 10);
	Person p2("李四", 15);
	Person p3("王五", 15);
	Person p4("赵六", 16);
	p.push_back(p1);
	p.push_back(p2);
	p.push_back(p3);
	p.push_back(p4);
	Person pf("张麻子", 15);
	int num = count(p.begin(), p.end(), pf);
	cout << "num=" << num << endl;
}

6.count_if()

cpp 复制代码
class Person
{
public:
	Person(string name,int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};
class MyCompare
{
public:
	bool operator()(const int val)
	{
		return val > 30;
	}
};
class AgeGreater14
{
public:
	bool operator()(const Person& p)
	{
		return p.m_Age > 14;//统计年龄大于14的人的个数
	}
};
void test01()//count_if统计内置类型
{
	vector<int> v1;
	v1.push_back(10);
	v1.push_back(20);
	v1.push_back(40);
	v1.push_back(30);
	v1.push_back(40);
	v1.push_back(40);
	v1.push_back(50);
	int num = count_if(v1.begin(), v1.end(), MyCompare());//统计大于30的个数
	cout << "大于30的个数是:" << num << endl;

}
void test02()//count_if统计自定义类型
{
	vector<Person> p;
	Person p1("张三", 10);
	Person p2("李四", 14);
	Person p3("王五", 15);
	Person p4("赵六", 16);
	p.push_back(p1);
	p.push_back(p2);
	p.push_back(p3);
	p.push_back(p4);
	int num = count_if(p.begin(), p.end(),AgeGreater15());
	cout << "num=" << num << endl;
}
相关推荐
艾莉丝努力练剑15 分钟前
【LeetCode&数据结构】单链表的应用——反转链表问题、链表的中间节点问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
_殊途2 小时前
《Java HashMap底层原理全解析(源码+性能+面试)》
java·数据结构·算法
还债大湿兄2 小时前
《C++内存泄漏8大战场:Qt/MFC实战详解 + 面试高频陷阱破解》
c++·qt·mfc
珊瑚里的鱼5 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上5 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
Risehuxyc5 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
秋说6 小时前
【PTA数据结构 | C语言版】顺序队列的3个操作
c语言·数据结构·算法
lifallen7 小时前
Kafka 时间轮深度解析:如何O(1)处理定时任务
java·数据结构·分布式·后端·算法·kafka
liupenglove7 小时前
自动驾驶数据仓库:时间片合并算法。
大数据·数据仓库·算法·elasticsearch·自动驾驶
python_tty8 小时前
排序算法(二):插入排序
算法·排序算法