algorithm 简单用法

algorithm 简单用法

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


int student_Score[] = { 50,80,93,23,66};

void pritit(int nScore)
{
	cout<<nScore<<" ";
}
bool unPass(int nScore)
{
	return nScore < 60;
}
bool Pass(int nScore)
{
	return nScore >= 60;
}

int main(int argc, char* argv[])
{
	vector<int> v_score(student_Score,student_Score+sizeof(student_Score)/sizeof(int));
	vector<int>::iterator index;

	sort(v_score.begin(),v_score.end()); 	//排序

	for_each(v_score.begin(),v_score.end(),pritit); cout<<endl;	//显示

	index = min_element(v_score.begin(),v_score.end());	//显示最小
	cout<<"最小分数 "<<*index<<endl;

	index = max_element(v_score.begin(),v_score.end());	//显示最大
	cout<<"最大分数 "<<*index<<endl;

	cout<<"低于60的数量 " <<count_if(v_score.begin(),v_score.end(),unPass)<<endl;	//显示低于60分的数量

	cout<<"高于60的数量 "<<count_if(v_score.begin(),v_score.end(),Pass)<<endl;	//高于60的数量

	int sum = 0;
	for (index = v_score.begin(); index != v_score.end(); index++)	//平均数
	{
		sum += *index;
	}
	cout<<"平均数 "<<sum / v_score.size() <<endl;

	return 0;
}


原文地址:https://www.cnblogs.com/gongpixin/p/4477430.html