WeiHandsome 2023-07-09 15:37 采纳率: 85%
浏览 20
已结题

c++ accumulate 计算自定义数据

大家主要看一下test02,假设一共10个人,用accumulate求一下平均年龄,该如何实现?
我觉得底层是+=操作,重载了+=号,好像没啥用



#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
#include<ctime>
#include<numeric>

class Person
{
public:
    Person()
    {

    }
    Person(string name, int age);
    ~Person();

    //int operator+=(int x)
    //{
    //    return this->m_Age += x;
    //}

    Person operator+=(Person tempt)
    {        
        this->m_Age+= tempt.m_Age ;
        return *this;
    }
private:
    string m_Name;
    int m_Age;

    friend void test02();
    friend class MyPrint;
};

Person::Person(string name, int age)
{
    this->m_Name = name;
    this->m_Age = age;
}

Person::~Person()
{

}

class MyPrint
{
public:
    void operator()(const Person& p)
    {
        cout << "濮撳悕" << p.m_Name << "骞撮緞" << p.m_Age << endl;
    }
};

void myPrint(int value)
{
    cout << value << " ";
}

void test01()
{
    vector<int>v;

    for (int i = 0; i < 101; i++)
    {
        v.push_back(i);
    }

    cout << "总数"<< endl;
    int sum = accumulate(v.begin(), v.end(), 1000);
    cout << sum << "\n";


}

void test02()
{

    vector<Person>v;
    char nameSeed[8][2] = { "A","B","C","D","E", "F", "G", "H" };
    srand((unsigned int)time(NULL));

    for (int i = 0; i < 8; i++)
    {
        int age = rand() % 100 + 1;
        v.push_back(Person((const char*)nameSeed[i], age));
    }



    Person pp("C", 0);
    Person ppp("D", 9999);
    v.push_back(pp);
    v.push_back(ppp);


    cout << "人员如下 :"<< endl;
    for_each(v.begin(), v.end(), MyPrint()); cout << endl;

    //int average = accumulate(v.begin(), v.end(), 0) / v.size();


}
int main()
{
    test01(); cout << endl;
    test02(); cout << endl;
    std::cout << "Hello World!\n";
}

  • 写回答

2条回答 默认 最新

  • Huazie 全栈领域优质创作者 2023-07-09 16:15
    关注

    先看截图:

    img

    修改如下:

     
     
    #include <iostream>
    #include<vector>
    #include<algorithm>
    #include<cstring>
    #include<ctime>
    #include<numeric>
    using namespace std;
    
     
    class Person
    {
    public:
        Person()
        {
     
        }
        Person(string name, int age);
        ~Person();
     
        Person operator+(Person tempt)  // 修改
        {        
            Person result;
            result.m_Name = this->m_Name;
            result.m_Age = this->m_Age + tempt.m_Age;
            return result;
        }
    
        int getAge() {  // 新增
            return m_Age;
        }
    
    private:
        string m_Name;
        int m_Age;
     
        friend void test02();
        friend class MyPrint;
    };
     
    Person::Person(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
     
    Person::~Person()
    {
     
    }
     
    class MyPrint
    {
    public:
        void operator()(const Person& p)
        {
            cout << "姓名:" << p.m_Name << "年龄:" << p.m_Age << endl;
        }
    };
     
    void myPrint(int value)
    {
        cout << value << " ";
    }
     
    void test01()
    {
        vector<int>v;
     
        for (int i = 0; i < 101; i++)
        {
            v.push_back(i);
        }
     
        cout << "总数"<< endl;
        int sum = accumulate(v.begin(), v.end(), 0);
        cout << sum << "\n";
     
     
    }
     
    void test02()
    {
     
        vector<Person>v;
        char nameSeed[8][2] = { "A","B","C","D","E", "F", "G", "H" };
        srand((unsigned int)time(NULL));
     
        for (int i = 0; i < 8; i++)
        {
            int age = rand() % 100 + 1;
            v.push_back(Person((const char*)nameSeed[i], age));
        }
     
     
     
        Person pp("C", 0);
        Person ppp("D", 9999);
        v.push_back(pp);
        v.push_back(ppp);
     
     
        cout << "人员如下 :"<< endl;
        for_each(v.begin(), v.end(), MyPrint()); cout << endl;
     
        Person result = accumulate(v.begin(), v.end(), Person("", 0)); // 修改
        int average = result.getAge() / v.size(); //修改
        cout<< average<<endl;   
     
    }
    int main()
    {
        test01(); cout << endl;
        test02(); cout << endl;
        std::cout << "Hello World!\n";
    }
     
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 7月9日
  • 已采纳回答 7月9日
  • 创建了问题 7月9日

悬赏问题

  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?