栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

内建函数对象和常用算法

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

内建函数对象和常用算法

1,stl函数对象的分类

a,算数仿函数
b,关系仿函数
c,逻辑仿函数
这些函数与一般函数相同,使用内建函数对象,需要引入头文件#include

2,算术仿函数

这里举两个例子plus和negate

#include
#include
using namespace std;

int main()
{
	negate p;//取反
	cout << p(50) << endl;
	plus c;//加法
	cout << c(10, 50) << endl;

	return 0;
}
3,关系仿函数

实现关系对比
greater()

#include
#include
using namespace std;
#include
#include<./algorithm>


int main()
{
	vector sh;
	sh.push_back(50);
	sh.push_back(40);
	sh.push_back(30);
	sh.push_back(80);
	sh.push_back(1000);
	sh.push_back(304);
	sh.push_back(500);
	sh.push_back(440);
	sh.push_back(303);
	sh.push_back(504);
	sh.push_back(407);
	sh.push_back(3090);
	sort(sh.begin(), sh.end(), greater());//匿名函数对象
	vector::iterator it = sh.begin();
	for (;it!=sh.end();it++)
	{
		cout << *it << endl;
	}



	return 0;
}
4,逻辑仿函数基本用不到。 5常用算法函数

for_each()

#include
#include
using namespace std;
#include
#include<./algorithm>

class print
{
public:
	void operator()(int val)
	{
		cout << val << endl;

	}


};

void myprint(int sh)
{
	cout <<  sh<< endl;

}
int main()
{
	vector sh;
	sh.push_back(50);
	sh.push_back(40);
	sh.push_back(30);
	sh.push_back(80);
	sh.push_back(1000);
	sh.push_back(304);
	sh.push_back(500);
	sh.push_back(440);
	sh.push_back(303);
	sh.push_back(504);
	sh.push_back(407);
	sh.push_back(3090);
	sort(sh.begin(), sh.end(), greater());
	for_each(sh.begin(),sh.end(),myprint);//函数
	for_each(sh.begin(),sh.end(),print());//匿名函数对象
	vector::iterator it = sh.begin();
	for (;it!=sh.end();it++)
	{
		cout << *it << endl;
	}



	return 0;
}
transform交换两个模板
#include
#include<./algorithm>
#include<./functional>
#include
using namespace std;

class Trans
{
public:
	int operator()(int val)
	{
		return val+10;
	}

};
void myprint(int val)
{
	cout<
	vector p;
	vectortarget;

	for (int i=0;i<10;i++)
	{
		p.push_back(i);
	}
	target.resize(p.size());

	for_each(p.begin(),p.end(),myprint);

	transform(p.begin(),p.end(),target.begin(),Trans());

	for_each(target.begin(), target.end(), myprint);
	return 0;
}
常用查找算法
#include
#include<./algorithm>
#include<./functional>
#include
using namespace std;
#include
//常用查找算法
//find,find_if,

class person
{
public:
	person(int age,string name)
	{
		this->m_age = age;
		this->m_name = name;
	}
	bool operator==(const person &a)//重载==
	{
		if (this->m_age == a.m_age && this->m_name == a.m_name)
		{
			return true;
		}
		else
		{
			return false;
		}
	
	}

public:
	int m_age;
	string m_name;

};
int main()
{
	person a(20,"name");
	person b(20, "h");
	person c(20, "ng");
	person d(20, "me");
	person e(100, "ame");
	person f(50, "namfe");
	person g(60, "namfae");

	vector vec;

	vec.push_back(a);
	vec.push_back(b);
	vec.push_back(c);
	vec.push_back(d);
	vec.push_back(e);
	vec.push_back(f);
	vec.push_back(g);

	person pp(20, "h");
	vector::iterator it = find(vec.begin(),vec.end(),pp);//查找指定容器数据

	cout<m_age<<" "<m_name << endl;
	return 0;
}

find_if
#include
#include<./algorithm>
#include<./functional>
#include
using namespace std;
#include

//常用查找算法

//find,find_if,

class person
{
public:
	person(int age,string name)
	{
		this->m_age = age;
		this->m_name = name;
	}
	int operator()(const person &a)
	{
		return a.m_age > 20;
	}
	
	

public:
	int m_age;
	string m_name;

};


class great
{
public:
	bool operator()(person& a)
	{
		return a.m_age > 20;
	}


};
int main()
{
	person a(20,"name");
	person b(20, "h");
	person c(20, "ng");
	person d(20, "me");
	person e(100, "ame");
	person f(50, "namfe");
	person g(60, "namfae");
	vector vec;
	vec.push_back(a);
	vec.push_back(b);
	vec.push_back(c);
	vec.push_back(d);
	vec.push_back(e);
	vec.push_back(f);
	vec.push_back(g);
	vector::iterator it = find_if(vec.begin(),vec.end(),great());//查找指定容器数据(条件成立)

	cout<m_age<<" "<m_name << endl;

	it++;
		cout << it->m_age << " " << it->m_name << endl;
		it++;
	cout << it->m_age << " " << it->m_name << endl;

	return 0;
}

adjacent_find()作用查找stl模板中是否有相同的元素
binary_find()作用查找stl模板中是否有此元素
count作用是统计次数
count_if按条件统计次数

常用排序算法

sort(const _RanIt _First, const _RanIt _Last, _Pr _Pred)默认为从小到大
rrandom_shuffle(_RanIt _First, _RanIt _Last)打乱次序
void reverse(const _BidIt _First, const _BidIt _Last)翻转次序
merge(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest, _Pr _Pred)合并两个容器

常用拷贝算法

copy(_InIt _First, _InIt _Last, _OutIt _Dest)//复制一个容器到另外一个容器
replace(const _FwdIt _First, const _FwdIt _Last, const _Ty & _Oldval, const _Ty & _Newval)//改变原来的旧值
replace_if(const _FwdIt _First, const _FwdIt _Last, _Pr _Pred, const _Ty & _Val)//按条件改变
swap(container c1,container c2)//交换两个容器

常用算术生成算法

accumulate()//计算容器中的总和
fill()//向容器中添加元素

常用集合算法

set_intersectoin()求两个容器里面的交集结果存储在第三个容器中
set_union()求两个容器并集结果存储在第三个容器中,必须有序
set_difference()求两个容器差集结果存储在第三个容器中,必须有序

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/942569.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号