STL-list&vector对自定义数据操作重载仿函数

本文介绍了如何在C++中为自定义数据类型如`Person`重载`==`和`<`操作符以进行查找和排序。通过重载`==`,实现了`list`容器中元素的正确移除;通过重载`<`,实现了`list`的排序。此外,还讨论了仿函数的概念,展示了如何使用普通函数和类的成员函数作为排序规则,并探讨了类内仿函数的实现问题。最后,通过`lambda`表达式展示了函数对象的另一种形式。
摘要由CSDN通过智能技术生成
  • 针对自定义数据
  • 查找,重载==操作符
  • 排序,重载<操作符
  • 注意源码的返回值,可以用成员函数重载,节省一个参数
  • 双向迭代器,不支持begin()+n操作,可以用it++,相等于it=it->next,重载了
  • list不能用系统的sort()算法–只支持随机迭代器。有自己的L.sort()
  • 指定排序规则L.sort(func)
  • 仿函数的本质:类的成员函数重载()操作符,对象()的形式像函数一样调用。
  • 类()是匿名函数
  • 函数名就是函数指针,可以当作参数传递

待解决问题:类内实现仿函数为什么不行

#include <iostream>
#include<string>
#include<list>
#include<vector>
#include<algorithm>
using namespace std;
class Person
{
public:
    Person(string name,int age) {
        this->name=name;
        this->age=age;
    }
public:
    string name;
    int age;
    //成员函数重载 ==运算符
    bool operator ==(const Person&ob)
    {
        if(this->name==ob.name&&this->age==ob.age)
            return true;
        return false;
    }
    //成员函数重载<操作符
    bool operator <(const Person&ob)
    {
        return this->age<ob.age;
    }
    bool operator()(Person &ob)//在类内不能实现仿函数
    {
        return this->age>ob.age;
    }
};
void  printListPerson(list<Person>&L)
{
     cout<<"-----------"<<endl;
    for(list<Person>::iterator it=L.begin();it!=L.end();it++)
    {
        cout<<(*it).name<<" "<<(*it).age<<endl;
    }
}
void test01()//重载==操作符
{
    list<Person>L;
    L.push_back(Person("的麦西亚",48));
    L.push_back(Person("提莫",28));
    L.push_back(Person("狗头", 18));
    L.push_back(Person("牛头", 19));
    printListPerson(L);

    Person tmp("狗头",18);
    L.remove(tmp);//err,不知道怎么==,所以重载==操作符
    printListPerson(L);

}
void test02()//重载<操作符
{
       list<Person> L;
       L.push_back(Person("德玛西亚",48));
       L.push_back(Person("提莫", 28));
       L.push_back(Person("狗头", 18));
       L.push_back(Person("牛头", 19));
       printListPerson(L);
       L.sort();//err,不知道怎么比较,所以重载<操作符
       printListPerson(L);

}
bool myComparePerson(Person&ob1,Person&ob2)
{
    return ob1.age>ob2.age;
}
void test03()//普通函数指定排序规则
{
    list<Person> L;
    L.push_back(Person("德玛西亚",48));
    L.push_back(Person("提莫", 28));
    L.push_back(Person("狗头", 18));
    L.push_back(Person("牛头", 19));
    printListPerson(L);
    L.sort(myComparePerson);
    printListPerson(L);
}
void PrintVectorPerson(vector<Person>&v)
{
    cout<<"-----------"<<endl;
    for(vector<Person>::iterator it=v.begin();it!=v.end();it++)
    {
        cout<<(*it).name<<" "<<(*it).age<<endl;
    }
}
class myComparePerson2
{
public:
    bool operator()(Person&ob1,Person&ob2)
    {
        return ob1.age>ob2.age;
    }
};
void test04()//仿函数指定排序规则//仿函数本质就是类重载了一个operator(),创建一个行为类似函数的对象
{
    vector<Person>v;
    v.push_back(Person("德玛西亚",48));

       v.push_back(Person("提莫", 28));
       v.push_back(Person("狗头", 18));
       v.push_back(Person("牛头", 19));
       PrintVectorPerson(v);
      //sort(v.begin(),v.end());//默认从小到大
       //sort(v.begin(),v.end(),myComparePerson);//普通函数指定排序规则
       sort(v.begin(),v.end(),myComparePerson2());//匿名对象,再加()是仿函数调用
       //sort(v.begin(),v.end(),Person());//类的内部为啥不行
      PrintVectorPerson(v);
}
class MyAd
{
public:
    int operator()(int a,int b)
    {
        return a+b;
    }
    int operator ()(int a,int b,int c)
    {
        return a+b+c;

    }
};
void test05()//仿函数练习,本质是类中的成员函数重载了一个operator(),创建一个行为类似函数的对象
{
    MyAd ob1;
    cout<<ob1.operator ()(2,3)<<endl;
    //编译器优化
        //严格意义:ob1是对象 和()结合 调用operator ()成员函数
        //ob1(100,200) ob1本质是【对象】不是函数名 只是形式像函数调用
        //叫做仿函数
    cout<<ob1(2,3)<<endl;
    cout<<ob1(1,2,3)<<endl;
    cout<<MyAd()(2,3)<<endl;
}
void myPrint(vector<int>&v)
{
    for(vector<int>::iterator it=v.begin();it!=v.end();it++)
    {
        cout<<(*it)<<" ";
    }
}
void print(int val)
{
    cout<<val<<" ";
}
void test06()//lamba表达式
{
    vector<int>v;
    v.push_back(10);
    v.push_back(20);     
    v.push_back(30);
    v.push_back(40);
   //方法1:访问v容器 普通函数
    myPrint(v);
     //方法2:访问v容器 系统算法for_each
    for_each(v.begin(),v.end(),print);
    //方法3:访问v容器 lambda表达式
        //[]表示函数名 ()参数列表 {}函数体
    for_each(v.begin(),v.end(),[](int val){
    cout<<val<<endl;
});

}
int main(int argc, char *argv[])
{
   //test01();
    //test02();
    //test03();
   // test04();
    //test05();
    test06();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值