13.C++常用的算法_查找算法

本文介绍了C++中四个常用的遍历和查找算法:adjacent_find用于查找相邻元素,binary_search执行二分查找,count统计元素个数,count_if则按特定条件计数。通过示例展示了这些函数在整数向量和自定义对象上的应用。
摘要由CSDN通过智能技术生成

遍历算法

1. adjacent_find

代码工程
查找相邻元素是否存在,不存在返回容器最后位置的迭代器
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

void printVector(const vector<int>&v)
{
	for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}


void test01()
{
	vector<int>v;

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

	printVector(v);

	/*查找相邻元素是否存在*/
	vector<int>::iterator pos = adjacent_find(v.begin(), v.end());

	if (pos != v.end())
	{
		cout << "找到相邻元素" << *pos << endl;
	}
	else
	{
		cout << "没有找到相邻元素" << endl;
	}


	return;
}

int main()
{
	test01();

	return 0;
}


运行结果

在这里插入图片描述

2. binary_search()

二分查找法,不加仿函数默认升序判断。下边程序是加仿函数的降序判断版本。
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

void printVector(const vector<int>&v)
{
	for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	vector<int>v;

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

	printVector(v);

	/*两分查找*/
	bool ret = binary_search(v.begin(), v.end(), 40, greater<int>());/*查找降序,不加内联函数默认查找升序*/

	if (ret == true)
	{
		cout << "找到元素" << endl;
	}
	else
	{
		cout << "没有找到元素" << endl;
	}

	return;
}

int main()
{
	test01();

	return 0;
}

运行结果

在这里插入图片描述

3. count()

返回值为符合条件个数
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

void printVector(const vector<int>&v)
{
	for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

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)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	string m_name;
	int m_age;
};

void test01()
{
	int num = 0;/*元素个数*/

	vector<int>v;

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

	printVector(v);

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

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

	return;
}

void test02()
{
	int num = 0;/*元素个数*/

	vector<Person>v;

	Person p1("赵云", 25);
	Person p2("刘备", 26);
	Person p3("曹操", 28);
	Person p4("张飞", 26);
	Person p5("关羽", 29);
	Person p6("黄忠", 30);

	Person pp("马超", 26);

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

	num = count(v.begin(), v.end(), pp);

	cout << "与马超年龄相同的人有" << num << "个" << endl;

	return;
}

int main()
{
	test01();

	cout << endl << "自定义元素类型查找" << endl;

	test02();

	return 0;
}


运行结果

在这里插入图片描述

4. count_if()

返回值为符合条件范围的个数
代码工程
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

void printVector(const vector<int>&v)
{
	for (vector<int>::const_iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

class Person
{
public:
	Person(string name, int age)
	{
		m_name = name;
		m_age = age;
	}

	string m_name;
	int m_age;
};

class GreaterTwenty
{
public:
	bool operator()(int val)
	{
		if (val > 20)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	bool operator()(const Person &p)/*重载Person入参类型*/
	{
		if (p.m_age > 20)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
};

void test01()
{
	int num = 0;/*元素个数*/

	vector<int>v;

	v.push_back(10);
	v.push_back(50);
	v.push_back(30);
	v.push_back(70);
	v.push_back(40);
	v.push_back(60);

	printVector(v);

	num = count_if(v.begin(), v.end(), GreaterTwenty());

	cout << "元素大于20的个数为:" << num << endl;

	return;
}

void test02()
{
	int num = 0;/*元素个数*/

	vector<Person>v;

	Person p1("赵云", 18);
	Person p2("刘备", 20);
	Person p3("曹操", 28);
	Person p4("张飞", 26);
	Person p5("关羽", 29);
	Person p6("黄忠", 19);

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

	num = count_if(v.begin(), v.end(), GreaterTwenty());

	cout << "年龄大于20的人有" << num << "个" << endl;

	return;
}

int main()
{
	test01();

	cout << endl << "自定义元素类型查找" << endl;

	test02();

	return 0;
}


运行结果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值