C++编程 STL算法 简单示例

目录

一:自带算法

二:自定义算法

三:函数对象

四:标准C++库中的算法

五:STL算法的头文件


一:自带算法

链表list自带算法,是按照地址排序的,示例如下

CStaff.h:

#ifndef CSTAFF_H
#define CSTAFF_H
#define ADMIN 1
#define MANAGER 2
#define WAITER 3
#include<string>
#include<iostream>
using namespace std;

class Staff
{
public:
	Staff();
	Staff(int id,string name,string pwd,int prole);
	~Staff();
	int getId();
	string getName();
	string getPwd();
	int getRole();
//	bool operator < (Staff &user);//隐式传参 this
private:
	int ID;
	string name;
	string pwd;
	int role;
};

#endif

CStaff.cpp: 

#include"CStaff.h"
#include<iostream>
using namespace std;

Staff::Staff()
{
}

Staff::Staff(int id,string name,string pwd,int prole)
{
	this->ID = id;
	this->name = name;
	this->pwd = pwd;
	this->role = prole;
}

int Staff::getId()
{
	return this->ID;
}

string Staff::getName()
{
	return this->name;
}

string Staff::getPwd()
{
	return this->pwd;
}

int Staff::getRole()
{
	return this->role;
}

Staff::~Staff()
{
}
/*
bool Staff::operator <(Staff &user)//函数排序
{
	if(this->ID<user.ID)           //从小到大
	{
		return true;
	}
	else
	{
		return false;
	}
}
*/

main.cpp: 

#pragma warning (disable :4786)
#include<iostream>
using namespace std;
#include<string>
#include<list>
#include"CStaff.h"
#include<functional>
#include<algorithm>

void demo_list();

int main()
{
	demo_list();
	return 0;
}

void demo_list()
{
	list<Staff*> staffV;
	staffV.push_back(new Staff(1001,"admin","123",ADMIN));
	staffV.push_back(new Staff(1003,"lily","123",ADMIN));
	staffV.insert(staffV.begin(),new Staff(1002,"admin","123",ADMIN));
	//1002 1001 1003
	list<Staff*>::iterator it;
	for(it=staffV.begin();it!=staffV.end();it++)
	{
		cout<<(*it)<<"---"<<(*it)->getId()<<endl;
	}
	staffV.sort();//地址排序
	cout<<"------------"<<endl;
	for(it=staffV.begin();it!=staffV.end();it++)
	{
	   cout<<(*it)<<"---"<<(*it)->getId()<<endl;
	}
/*按地址排序
01FA8558---1002
01FA8198---1001
01FA87B0---1003
------------
01FA8198---1001
01FA8558---1002
01FA87B0---1003
*/
}

二:自定义算法

1. 自定义按照Id排序

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include"CStaff.h"
#include<functional>//函数对象
#include<algorithm> //算法

bool sortByName(Staff staff1,Staff staff2) //自定义姓名排序
{
	if(staff1.getName()<staff2.getName())
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool sortById(Staff staff1,Staff staff2)  //自定义Id排序
{
	if(staff1.getId()<staff2.getId())
	{
		return true;
	}
	else
	{
		return false;
	}
}

void demo_vector()
{
	vector<Staff> staffV;
	staffV.push_back(Staff(1001,"admin","123",ADMIN));
	staffV.push_back(Staff(1003,"lily","123",ADMIN));
	staffV.push_back(Staff(1002,"banana","123",ADMIN));
	staffV.push_back(Staff(1004,"candy","123",ADMIN));
	vector<Staff>::iterator it;
	for(it=staffV.begin();it!=staffV.end();it++)
	{
		cout<<(*it).getId()<<endl;
	}
	sort(staffV.begin(),staffV.end(),sortById);
	cout<<"----------"<<endl;
	for(it=staffV.begin();it!=staffV.end();it++)
	{
		cout<<(*it).getId()<<endl;
	}
}
/*
1001
1003
1002
1004
----------
1001
1002
1003
1004
*/
int main()
{
   demo_vector();
   return 0;
}

2. 自定义按照姓名排序

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include"CStaff.h"
#include<functional>
#include<algorithm>

bool sortByName(Staff staff1,Staff staff2)
{
	if(staff1.getName()<staff2.getName())
	{
		return true;
	}
	else
	{
		return false;
	}
}

void demo_vector()
{
	vector<Staff> staffV;
	staffV.push_back(Staff(1001,"admin","123",ADMIN));
	staffV.push_back(Staff(1003,"lily","123",ADMIN));
	staffV.push_back(Staff(1002,"banana","123",ADMIN));
	staffV.push_back(Staff(1004,"candy","123",ADMIN));
	vector<Staff>::iterator it;
	for(it=staffV.begin();it!=staffV.end();it++)
	{
		cout<<(*it).getId()<<":"<<(*it).getName().c_str()<<endl;
	}
	sort(staffV.begin(),staffV.end(),sortByName);
	cout<<"----------"<<endl;
	for(it=staffV.begin();it!=staffV.end();it++)
	{
		cout<<(*it).getId()<<":"<<(*it).getName().c_str()<<endl;
	}
}
/*
1001:admin
1003:lily
1002:banana
1004:candy
----------
1001:admin
1002:banana
1004:candy
1003:lily
*/
int main()
{
    demo_vector();
	return 0;
}

三:函数对象

1. 任何普通的函数和任何重载了调用运算符operator()的类的对象都满足函数对象的特征

2. STL中也定义了一些标准的函数对象,如果以功能划分,可以分为算术运算、关系运算、

逻辑运算三大类;为了调用这些标准函数对象,需要包含头文件<functional>

四:标准C++库中的算法

1.算法本身是一种函数模板

2.不可变序列算法(non-mutating algorithms)

   不直接修改所操作的容器内容的算法

3.可变序列算法(mutating algorithms)

   可以修改它们所操作的容器的元素。

4.算法部分主要由头文件<algorithm>,<numeric>和<functional>组成

五:STL算法的头文件

1. <algorithm>是所有STL头文件中最大的一个,它是由一大堆模版函数组成的,可以认为每个函数在很大程度上都是独立的,其中常用到的功能范围涉及到比较、交换、查找、遍历操作、复制、修改、移除、反转、排序、合并等等

2. <numeric>体积很小,只包括几个在序列上面进行简单数学运算的模板函数,包括加法和乘法在序列上的一些操作

3. <functional>中则定义了一些模板类,用以声明函数对象 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenruhan_QAQ_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值