STL算法

本文介绍了C++中的STL算法,包括使用for_each进行遍历,find_if进行条件查找,以及自定义bsort函数进行冒泡排序。示例代码展示了如何结合模板函数和仿函数实现个性化的操作,如表白和特定元素查找,并提供了升序和降序排序的实现方式。
摘要由CSDN通过智能技术生成

1、for_each()遍历

示例(foreach):
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using  namespace std;

template<typename T>
void zsshow(const T& no)    // 张三的个性化表白函数。
{
	cout << "亲爱的" << no << "号:我是一只傻傻鸟。\n";
}

template<typename T>
class czs   // 张三的个性化表白仿函数。
{
public:
	void operator()(const T& no) {  
		cout << "亲爱的" << no << "号:我是一只傻傻鸟。\n";
	}
};

template<typename T1, typename T2>
void foreach(const T1 first, const T1 last, T2 pfun)
{
	for (auto it = first; it != last; it++)
		pfun(*it);        // 以超女编号为实参调用类的operator()函数。
}

int main()
{
	vector<int> bh = { 5,8,2,6,9,3,1,7 };   // 存放超女编号的容器。
	//list<string> bh = { "05","08","02","06","09","03","01","07" };   // 存放超女编号的容器。

	// 写一个函数,在函数中遍历容器,向超女表白,表白的方法可自定义。
	foreach(bh.begin(), bh.end(), zsshow<int>);  // 第三个参数是模板函数。
	
	foreach(bh.begin(), bh.end(), czs<int>());       // 第三个参数是仿函数。
}

2、find_if

#pragma region find_if
template<typename T>
bool findLike(const T& no) {
	if (no == 3)
	{
		cout << "亲爱的" << no << "我找到你了" << endl;
		return true;
	}
	else
	{
		return false;
	}
}


template<typename T>
class czs2
{
public:
	T t_no;
	czs2(const T & no):t_no(no){}
	bool operator()(const T& no) {
		if (no ==t_no)
		{
			cout << "亲爱的" << no << "我找到你了" << endl;
			return true;
		}
		else
		{
			return false;
		}
	}

private:

};
template<typename T1,typename T2>
T1 findif(const T1 first, const T1 last, T2 pfun) {
	for (auto it = first; it != last; it++)
		if (pfun(*it)==true)  return it;

	return last;
}

#pragma endregion


int main() {



	list<int> bh = { 5,3,6,1 };
	auto it1= find_if(bh.begin(), bh.end(), findLike<int>);
	if (it1 == bh.end()) cout << "查找失败" << endl;
	else   cout << "查找成功" <<*it1<< endl;

	auto it2 = find_if(bh.begin(), bh.end(), czs2<int>(3));
	if (it1 == bh.end()) cout << "查找失败" << endl;
	else   cout << "查找成功" << *it1 << endl;

	system("pause");


}

3、bsort

//示例(bsort):
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>   // STL算法函数。
#include <functional>   // STL仿函数。
using  namespace std;

template<typename T>
bool compasc(const T& left, const T& right)   {     // 普通函数,用于升序。
	return left < right;
}

template<typename T>
struct _less
{
	bool operator()(const T& left, const T& right) { // 仿函数,用于升序。
		return left < right;
	}
};

template<typename T>
bool compdesc(const T& left, const T& right) {     // 普通函数,用于降序。
	return left > right;
}

template<typename T>
class _greater
{
public:
	bool operator()(const T& left, const T& right) { // 仿函数,用于降序。
		return left > right;
	}
};

template<typename T, typename compare>
void bsort(const T first, const T last, compare comp)  // 冒泡排序。
{
	while(true)
	{
		bool bswap = false;        // 本轮遍历已交换过元素的标识,true-交换过,false-未交换过。
		for (auto it = first; ; )
		{
			auto left = it;                  // 左边的元素。
			it++;
			auto right = it;               // 右边的元素。
			if (right == last) break;  // 表示it1已经是最后一个元素了。

			//if (*left > *right)             // 如果左边的元素比右边大,交换它们的值。
			//if (*left < *right)             // 如果左边的元素比右边小,交换它们的值。
			// 排序规则:如果comp()返回true,left排在前面(升序),否则right排在前面(降序)。
			if (comp(*left, *right) == true)  continue;

			// 交换两个元素的值。
			auto tmp = *right;
			*right = *left;
			*left = tmp;
			bswap = true;        // 一轮遍历已交换过元素的标识。
		}

		if (bswap == false) break;  // 如果在for循环中不曾交换过元素,说明全部的元素已有序。
	}
}

int main()
{
	vector<int> bh = { 5,8,2,6,9,33,1,7 };   // 存放超女编号的容器。
	//list<string> bh = { "05","08","02","06","09","03","01","07" };   // 存放超女编号的容器。

	//bsort(bh.begin(), bh.end(),compasc<int>);        // 普通函数(升序)。
	//bsort(bh.begin(), bh.end(), compdesc<int>);     // 普通函数(降序)。

	//bsort(bh.begin(), bh.end(),_less<int>());             // 仿函数(升序)。
	//bsort(bh.begin(), bh.end(), _greater<int>());      // 仿函数(降序)。

	//bsort(bh.begin(), bh.end(), less<int>());             // STL提供的仿函数(升序)。
	//bsort(bh.begin(), bh.end(), greater<int>());       // STL提供的仿函数(降序)。

	//sort(bh.begin(), bh.end(),_less<int>());             // 仿函数(升序)。
	sort(bh.begin(), bh.end(), _greater<int>());      // 仿函数(降序)。

	for (auto val : bh)
		cout << val << " ";
	cout << endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值