非变异算法

for_each算法的使用 掌握查询算法find

  1. 求所有学生某门课程成绩的和和平均值,以vector为容器,利用函数对象和for_each算法实现。
  2. 查找学生成绩为80的学生信息,以vector为容器要求显示格式为“姓名:张三; 学号:001;成绩:80;”。 ,
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;


class Student
{
private:
    string m_strNO;         //学号
    string m_strName;       //姓名
    int m_chinesegrade;     //语文
    int m_mathgrade;        //数学

public:
    Student(string strNO,string strName,int ChineseGrade,int MathGrade):
            m_strNO(strNO),m_strName(strName),
            m_chinesegrade(ChineseGrade),m_mathgrade(MathGrade)
            {
            }

    string GetNO() {return m_strNO;}
    string GetName() {return m_strName;}
    int GetChineseGrade() {return m_chinesegrade;}
    int    GetMathGrade(){return m_mathgrade;}
    bool operator == (int grade)
    {
        if((this->GetChineseGrade() == grade) || (this->GetMathGrade() == grade))
        {
            return true;
        }
    }
};

ostream& operator << (ostream& os, Student& s)
{
os << s.GetNO() << "\t" << s.GetName() << "\t" << s.GetChineseGrade() << "\t" <<s.GetMathGrade();
return os;
}

class PrintIofo
{
private:
    int m_Chinesenum = 0;
    int m_Mathnum = 0;
    int count = 1;
    int avgChinese;
    int avgMath;
public:
    PrintIofo():count(1){}
    int GetChinesnum(){return m_Chinesenum;}
    int GetManthnum(){return m_Mathnum;}
    int Getcount(){return count;}
    int GetavgChines(){return avgChinese;}
    int GetavgMath(){return avgMath;}

    void operator()(Student& s)
    {
        m_Chinesenum += s.GetChineseGrade();
        m_Mathnum += s.GetMathGrade();

        avgChinese = m_Chinesenum/count;
        avgMath = m_Mathnum/count;

        count++;
    }

};

typedef vector<Student> LISTSTUD;

int main()
{
    LISTSTUD sm1;

    Student s1("001", "张三",80,90);
    Student s2("002", "李四",70,80);
    Student s3("003", "王五",60,90);

    sm1.push_back(s1);
    sm1.push_back(s2);
    sm1.push_back(s3);

    PrintIofo p = for_each(sm1.begin(), sm1.end(),PrintIofo());
    cout << "语文总成绩 "<<p.GetChinesnum() << "\t"
         << "语文平均成绩"<< p.GetavgChines() << endl
         << "数学总成绩 " << p.GetManthnum() << "\t"
         << "数学平均成绩" << p.GetavgMath()<< endl<<endl;

    LISTSTUD::iterator it_find;
    it_find = find(sm1.begin(),sm1.end(),80);


    if((*it_find).GetChineseGrade() == 80)
    {//姓名:张三; 学号:001;成绩:80;
      cout << "姓名:" << (*it_find).GetName() << "\t学号:" << (*it_find).GetNO()
           << "\t语文成绩为:" << (*it_find).GetChineseGrade()<<endl;
    }else if((*it_find).GetMathGrade() == 80)
    {
      cout << "姓名:" << (*it_find).GetName() << "\t学号:" << (*it_find).GetNO()
           << "\t语文成绩为:" << (*it_find).GetMathGrade()<<endl;
    }

}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值