首页 > 编程语言 >c++学习记录 STL—常用查找算法

c++学习记录 STL—常用查找算法

时间:2024-03-19 17:32:34浏览次数:19  
标签:end iterator STL back c++ Person 查找 push include

一、算法简介

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

二、find

1、功能描述

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

2、函数原型

  • find(iterator beg,iterator end,value);     //按值查找元素
  • // value 查找的元素
 #define _CRT_SECURE_NO_WARNINGS 1 
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>

//查找内置数据类型
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_Name = name;
		this->m_Age = age;
	}

	//重载底层 使底层find知道如何对比Person数据类型
	bool operator==(const Person& p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	string m_Name;
	int m_Age;
};

void test02()
{
	vector<Person>v;
	//创建数据
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);

	//放入到容器中
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	Person pp("bbb", 20);

	vector<Person>::iterator it = find(v.begin(), v.end(), pp);
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到元素 姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
	}
}

int main()
{
	test01();

	test02();

	system("pause");
	return 0;
}

三、find_if

1、功能描述

  • 按条件查找元素

2、函数原型

  • find_if(iterator beg,iterator end,_Pred);      //按条件查找元素
  • // _Pred 函数或谓词(返回bool类型的仿函数)
 #define _CRT_SECURE_NO_WARNINGS 1 
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>

//1、查找内置数据类型
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);
	}

	vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterFive());

	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到大于5的数字为:" << *it << endl;
	}
}

//2、查找自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age;
};

class Greater20
{
public:
	bool operator()(Person& p)
	{
		return p.m_Age > 20;
	}
};

void test02()
{
	vector<Person>v;

	//创建数据
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);

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

	//找年龄大于20的人
	vector<Person>::iterator it = find_if(v.begin(), v.end(), Greater20());

	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到 姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl;
	}
}

int main()
{
	test01();

	test02();

	system("pause");
	return 0;
}

四、adjacent_find

1、功能描述

  • 查找相邻重复元素

2、函数原型

  • adjacent_find(iterator beg,iterator end);      
  • // 查找相邻重复元素,返回相邻元素的第一个位置的迭代器
  • // beg 开始迭代器
  • // end 结束迭代器
 #define _CRT_SECURE_NO_WARNINGS 1 
#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>

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

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


int main()
{
	test01();

	system("pause");
	return 0;
}

五、binary_search

1、功能描述

  • 查找指定元素是否存在

2、函数原型

  • bool binary_search(iterator beg,iterator end,value);
  • // 查找指定元素,查到 返回true,否则 false
  • // 注意:在无序序列中不可用
 #define _CRT_SECURE_NO_WARNINGS 1 
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>

void test01()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	//查找容器中是否有9元素
	//注意:容器必须是有序的序列
	//v.push_back(2);  //如果是无序序列,结果未知!
	bool ret = binary_search(v.begin(), v.end(), 9);
	if (ret)
	{
		cout << "找到了元素" << endl;
	}
	else
	{
		cout << "未找到" << endl;
	}
}

int main()
{
	test01();

	system("pause");
	return 0;
}

六、count

1、功能描述

  • 统计元素个数

2、函数原型

  • count(iterator beg,iterator end,value);
  • // 统计元素出现次数
 #define _CRT_SECURE_NO_WARNINGS 1 
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>

//1、统计内置数据类型
void test01()
{
	vector<int>v;

	v.push_back(10);
	v.push_back(40);
	v.push_back(30);
	v.push_back(40);
	v.push_back(20);
	v.push_back(40);

	int num = count(v.begin(), v.end(), 40);

	cout << "40的元素个数为:" << num << endl;
}

//2、自定义数据类型
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;
		}
		else
		{
			return false;
		}
	}

	string m_Name;
	int m_Age;
};

void test02()
{
	vector<Person>v;

	Person p1("刘备", 35);
	Person p2("关羽", 35);
	Person p3("赵云", 35);
	Person p4("张飞", 30);
	Person p5("曹操", 40);

	//将人员插入到容器中
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	Person p("诸葛亮", 35);

	int num = count(v.begin(), v.end(), p);
	cout << "和诸葛亮同岁数的人员个数为:" << num << endl;
}

int main()
{
	test01();

	test02();

	system("pause");
	return 0;
}

七、count_if

1、功能描述

  • 按条件统计元素个数

2、函数原型

  • count_if(iterator beg,iterator end,_Pred);
  • // _Pred 谓词
 #define _CRT_SECURE_NO_WARNINGS 1 
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>

//统计内置数据类型
class Greater20
{
public:
	bool operator()(int val)
	{
		return val > 20;
	}
};

void test01()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(40);
	v.push_back(30);
	v.push_back(40);
	v.push_back(20);
	v.push_back(40);

	int num = count_if(v.begin(), v.end(), Greater20());
	cout << "大于20的元素个数为:" << num << endl;
}

//统计自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_Age;
};

class AgeGreater20
{
public:
	bool operator()(const Person& p)
	{
		return p.m_Age > 20;
	}
};

void test02()
{
	vector<Person>v;

	Person p1("刘备", 35);
	Person p2("关羽", 35);
	Person p3("赵云", 35);
	Person p4("张飞", 30);
	Person p5("曹操", 40);

	//将人员插入到容器中
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);

	//统计大于20岁人员个数
	int num = count_if(v.begin(), v.end(), AgeGreater20());
	cout << "大于20岁人员个数为:" << num << endl;
}

int main()
{
	test01();

	test02();

	system("pause");
	return 0;
}

标签:end,iterator,STL,back,c++,Person,查找,push,include
From: https://blog.csdn.net/weixin_74361062/article/details/136816381

相关文章

  • C++ kmalloc、kzalloc、vmalloc的区别
    1.kmalloc函数原型:void*kmalloc(size_tsize,gfp_tflags);kmalloc()申请的内存位于物理内存映射区域,而且在物理上也是连续的,它们与真实的物理地址只有一个固定的偏移,因为存在较简单的转换关系,所以对申请的内存大小有限制,不能超过128KB。较常用的flags(分配内存的方法):G......
  • 高质量C/C++编程指南
    目录        1、概述    本文档参考《高质量C++C编程指南》及自己的心得编写,如有侵权,立刻删除!2、编程指南2.1文章结构    每个C++/C程序通常分为两个文件。一个文件用于保存程序的声明,我们称之为头文件。另一个文件用于保存程序的实现,我们称......
  • Linux根据服务查找端口的方法
    1.用ps-ef|grep服务名查找进程号,以查询tomcat服务为例,查询出来的进程号为553002.用netstat-anop|grep进程号方式查询端口,得知该端口为:90903.也可用端口号使用命令 losf -i:端口号查询该端口是否存在服务进程......
  • STL:vector中如何使用at()来避免程序报错
     #include<iostream>#include<vector>usingnamespacestd;intmain(){ vector<int>Vec; for(inti=0;i<30;i++) { Vec.push_back(i); //cout<<Vec.size()<<endl; //cout<<Vec.capacity()<......
  • c++线程池(二)——线程池优化
    文章目录概要整体架构流程技术细节小结概要增加扇入扇出:优化:子线程维护自己的本地队列分析:目前文章《线程池一》介绍了一个简单的线程池,存在多个线程同时访问一个任务队列Task,出现抢锁的情况,这样会存在一定的性能消耗,会导致有些没抢到任务的线程没事做,造成资源浪......
  • 06_C++多维数组
    多维数组,数组指针在二维数组上的应用。#include<iostream>#include<stdio.h>usingnamespacestd;intmain(){intarr[3][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}};int(*p)[5]=arr;cout<<"*p:"<<*p<<endl......
  • 二分查找法 - C语言
    二分查找法比如我买了件300以下的衣服,你好奇,想知道到底多少钱,我让你猜,你会怎么猜呢?答案:你每次会猜中间数,不会从1开始猜。#include<stdio.h>intmain()//二分查找法(折半查找法){ intleft=0; intmid=0; intn=0; intarr[]={1,2,3,4,5,6,7,8,9,10}; ......
  • 分月饼【华为OD机试JAVA&Python&C++&JS题解】
    一.题目-分月饼中秋节,公司分月饼,m个员工,买了n个月饼,m<=n,每个员工至少分1个月饼,但可以分多个,单人分到最多月饼的个数是Max1,单人分到第二多月饼个数是Max2,Max1-Max2<=3,单人分到第n-1多月饼个数是Max(n-1),单人分到第n多月饼个数是Max(n),Max(n-1)–Max(n)<=3,问有多少......
  • 查找事物处理来源
    CREATEORREPLACEFUNCTIONcux_trans_source(p_trans_idNUMBER)RETURNVARCHAR2ISln_type_idNUMBER;ln_source_line_idNUMBER;ln_trx_source_line_idNUMBER;ln_source_type_idNUMBER;ln_transaction_source_idNUMBER......
  • C++实现欧拉筛法
    Euler筛法介绍以筛出100以内(含100)的所有素数为例来说明一下欧拉筛法的原理。和Eratosthenes筛法一样,Euler筛法也从2开始筛,但Eratosthenes筛法会把2的倍数一批全部筛掉,而Euler筛法用2筛时仅仅把2*2(即4)筛掉,而把其它偶数留到后面再筛掉,从而避免了一个偶数被多次筛除带来的性能开销,......