C++进阶STL-常用的查找算法

本文深入探讨了C++标准模板库(STL)中的多种查找算法,包括find、find_if、binary_search、adjacent_find、count及count_if的使用方法与自定义条件实现。通过具体代码示例,详细解析了如何在不同场景下应用这些算法,如查找特定对象、满足条件的第一个元素、二分查找、查找相邻重复元素以及计算符合特定条件的元素数量。
部署运行你感兴趣的模型镜像

#include <algorithm>

find

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


class Person
{
public:
	Person(int age, int id) :m_age(age), m_id(id)
	{

	}
	bool operator==(const Person& p)const  //使用find算法的时候在查找自定义对象需要 重载 == 操作符
	{
		return m_age == p.m_age&&m_id == p.m_id;
	}

	int m_age;
	int m_id;
};


int main()
{
	vector<Person> v1;
	Person p1(1, 1), p2(2, 2), p3(3, 3), p4(4, 4);
	v1.push_back(p1);
	v1.push_back(p2);
	v1.push_back(p3);
	v1.push_back(p4);

	vector<Person>::iterator it = find(v1.begin(),v1.end(), p2);
	if (it == v1.end())
	{
		cout << "not found" << endl;
	}
	else
	{
		cout << "found" << endl;
	}

    return 0;
}


find_if(会根据我们的条件(函数),返回第一个满足条件的元素的迭代器)

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


class Person
{
public:
	Person(int age, int id) :m_age(age), m_id(id)
	{

	}

	int m_age;
	int m_id;
};

struct Compare
{
	bool operator()(const Person&p1)
	{
		return p1.m_age >4;
	};
};


int main()
{
	vector<Person> v1;
	Person p1(1, 1), p2(2, 2), p3(3, 3), p4(4, 4),p5(5,5);

	v1.push_back(p1);
	v1.push_back(p2);
	v1.push_back(p3);
	v1.push_back(p4);
	v1.push_back(p4);
	v1.push_back(p5);

	vector<Person>::iterator it =find_if(v1.begin(),v1.end(),Compare());
	if (it == v1.end())
	{
		cout << "not found" << endl;
	}
	else
	{
		cout << "found:" <<(*it).m_age<< endl;
	}
	
    return 0;
}

结果:5



binary_search:二分法查找(必须有序排列的

  • 进行对象查找,自定义compare函数,如果是正常类型就不用提供compare,conpare可以是普通函数,也可以是函数对象(仿函数)
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


class Person
{
public:
	Person(int age, int id) :m_age(age), m_id(id)
	{
	}
	int m_age;
	int m_id;
};

struct Compare
{
	bool operator()(const Person&p1, const Person& p2)
	{
		return p1.m_age < p2.m_age;
	};
};

int main()
{
	vector<Person> v1;
	Person p1(1, 1), p2(2, 2), p3(3, 3), p4(4, 4),p5(5,5);

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

	bool tmp=binary_search(v1.begin(),v1.end(), p5, Compare());
	if (tmp == false)
	{
		cout << "not found" << endl;
	}
	else
	{
		cout << "found" << endl;
	}

    return 0;
}

结果:not found



adjacent_find:查找相邻重复的元素

  • 如果进行自定义类型查找,要么重载 == 操作符,要么需要给adjacent_find提供函数对象(仿函数)或者普通函数
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


class Person
{
public:
	Person(int age, int id) :m_age(age), m_id(id)
	{

	}
	bool operator==(const Person& p)const
	{
		return m_age == p.m_age&&m_id == p.m_id;
	}

	int m_age;
	int m_id;
};

//struct Compare
//{
//	bool operator()(const Person&p1, const Person& p2)
//	{
//		return p1.m_age == p2.m_age && p1.m_id==p2.m_id;
//	};
//};



int main()
{
	vector<Person> v1;
	Person p1(1, 1), p2(2, 2), p3(3, 3), p4(4, 4),p5(5,5);

	v1.push_back(p1);
	v1.push_back(p2);
	v1.push_back(p3);
	v1.push_back(p4);
	v1.push_back(p4);
	v1.push_back(p5);


	vector<Person>::iterator it =adjacent_find(v1.begin(),v1.end());
	if (it == v1.end())
	{
		cout << "not found" << endl;
	}
	else
	{
		cout << "found:" <<(*it).m_age<< endl;
	}

    return 0;
}

结果:4


count:计算符合条件(可以自定义)的个数

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


class Person
{
public:
	Person(int age, int id) :m_age(age), m_id(id)
	{

	}
	bool operator == (const Person& p2) const
	{
		return m_age == p2.m_age && m_id == p2.m_id;
	}
	int m_age;
	int m_id;
};

struct Compare
{
	bool operator()(const Person&p1)
	{
		return p1.m_age ==4;
	};
};


int main()
{
	vector<Person> v1;
	Person p1(1, 1), p2(2, 2), p3(3, 3), p4(4, 4),p5(5,5);

	v1.push_back(p4);
	v1.push_back(p5);
	v1.push_back(p1);
	v1.push_back(p2);
	v1.push_back(p3);
	v1.push_back(p4);
	v1.push_back(p4);
	v1.push_back(p5);


	int num =count(v1.begin(),v1.end(),p4);

	cout << num << endl;

    return 0;
}


结果:3



count_if:返回满足条件(可自定义)的元素个数

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


class Person
{
public:
	Person(int age, int id) :m_age(age), m_id(id)
	{

	}
	bool operator == (const Person& p2) const
	{
		return m_age == p2.m_age && m_id == p2.m_id;
	}
	int m_age;
	int m_id;
};

struct Compare
{
	bool operator()(const Person&p1)
	{
		return p1.m_age >3;
	};
};


int main()
{
	vector<Person> v1;
	Person p1(1, 1), p2(2, 2), p3(3, 3), p4(4, 4),p5(5,5);


	v1.push_back(p4);
	v1.push_back(p5);
	v1.push_back(p1);
	v1.push_back(p2);
	v1.push_back(p3);
	v1.push_back(p4);
	v1.push_back(p4);
	v1.push_back(p5);


	int num =count_if(v1.begin(),v1.end(),Compare());

	cout << num << endl;

    return 0;
}


结果:5

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值