栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++入门笔记-stl函数对象/谓词/内建函数对象

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++入门笔记-stl函数对象/谓词/内建函数对象

函数对象

概念:

        重载函数调用操作符的类 其对象常称为函数对象

        函数对象使用重载的()时 行为类似函数调用 也叫仿函数

本质:

        函数对象(仿函数)是一个类 不是一个函数

函数对象使用特点:

        函数对象在使用时 可以像普通函数那样调用 可以有参数和返回值

        函数对象超出了普通函数的概念 函数对象可以有自己的状态

        函数对象可以作为参数传递

#include 
using namespace std;

//函数对象在使用时 可以像普通函数那样调用 可以有参数和返回值
class MyAdd{
public:
    int operator()(int v1, int v2){
        return v1 + v2;
    }
};
void test01(){
    MyAdd ad;
    cout << ad(10,10) << endl;
}

//函数对象超出了普通函数的概念 函数对象可以有自己的状态
class MyPrint{
public:
    MyPrint(){
        this->count = 0;
    }
    void operator()(string test){
        cout << test << endl;
        this->count++;
    }
    int count; //内部自己的状态
};
void test02(){
    MyPrint mp;
    mp("hello world");
    mp("c++");
    
    //因为mp是一个类 所以可以通过count来记录自身的状态
    //而普通函数不是一个类 必须通过静态成员或者全局变量来实现
    cout << "MyPrint函数调用的次数为:" << mp.count << endl;
}

//函数对象可以作为参数传递
void doPrint(MyPrint &mp, string test){
    mp(test);
}
void test03(){
    MyPrint mp;
    doPrint(mp, "hello");
}
int main() {
    test01();
    test02();
    test03();
    return 0;
}

输出结果:

20

hello world

c++

MyPrint函数调用的次数为:2

hello

谓词 

概念:

        返回bool类型的仿函数称为谓词

        如果operator()接受一个参数 那么叫做一元谓词

        如果operator()接受两个参数 那么叫做二元谓词

一元谓词:

#include 
using namespace std;
#include 
#include 

class GreaterFive{
public:
    bool operator()(int val) //一个参数 所以是一元谓词
    {
        return val >5;
    }
};
void test01(){
    vectorv;
    
    for(int i=0; i<10; i++)
    {
        v.push_back(i);
    }
    
    //查找容器中有没有大于5的数字
    //GreaterFive() 匿名的函数对象
    //find_if中的i第三个参数是_Pr_Pred 也就是谓词的意思
    vector::iterator it = find_if(v.begin(),v.end(),GreaterFive());
    
    if(it == v.end()){
        cout << "未找到" << endl;
    }else{
        cout << "找到了第一个大于5的数字:" << *it << endl;
    }
}
int main() {
    test01();
    return 0;
}

输出结果:

找到了第一个大于5的数字:6

 二元谓词:

#include 
using namespace std;
#include 
#include 

class MyCompare{
public:
    //两个参数 所以是二元谓词
    bool operator()(int val1, int val2)
    {
        return val1 > val2;
    }
};
void printVec(vector &v){
    for(vector::iterator it = v.begin();it != v.end(); it++){
        cout << *it << "  ";
    }
    cout << endl;
}
void test01(){
    vectorv;
    
    v.push_back(10);
    v.push_back(50);
    v.push_back(20);
    v.push_back(15);
    v.push_back(30);
    
    //用标准算法sort排序 默认是升序
    sort(v.begin(),v.end());
    printVec(v);
    
    //使用仿函数改变算法策略 变为从大到小
    sort(v.begin(),v.end(),MyCompare());
    
    cout << "-------------------" << endl;
    printVec(v);
    
}
int main() {
    test01();
    return 0;
}

 输出结果:

10  15  20  30  50  

-------------------

50  30  20  15  10  

内建函数对象

概念:

        STL内建了一些函数对象

分类:

        算数仿函数

        关系仿函数

        逻辑仿函数

用法:

        这些仿函数所产生的对象 用法和一般函数完全相同

        使用内建函数对象 需要引入头文件 #include

算数仿函数:

      功能: 

        实现四则运算

        其中negate是一元运算 其他都是二元运算

      仿函数原型:

        template T plus  //加法

        template T minus  //减法

        template T multiplies  //乘法

        template T divides  //除法

        template T modulus  //取模

        template T negate  //取反

#include 
using namespace std;
#include 

void test01(){
    //取反仿函数
    negaten;
    
    cout << n(50) << endl;
}

void test02(){
    //这里虽然相加是二元谓词 但是只传一个int就可以了
    //因为加法要求两个数相加 数据类型要保持一致
    
    plusp;
    
    cout << p(10,20) << endl;
}
//其他的减乘除和加法用法一样 不做演示

int main() {
    test01();
    test02();
    return 0;
}

输出结果:

-50

30

关系仿函数:

      功能描述:

        实现关系对比

      仿函数原型:

        template bool equal_to  //等于

        template bool not_equal_to  //不等于

        template bool greater  //大于

        template bool greater_equal  //大于等于

        template less  //小于

        template less_equal  //小于等于

#include 
using namespace std;
#include 
#include 
#include 

void printVec(vector &v){
    for(vector::iterator it = v.begin();it != v.end();it++){
        cout << *it << " ";
    }
    cout << endl;
}
void test01(){
    vectorv;
    
    v.push_back(1);
    v.push_back(3);
    v.push_back(4);
    v.push_back(2);
    
    printVec(v);
    
    //使用内建的仿函数创建对象实现将序
    sort(v.begin(),v.end(),greater());
    
    printVec(v);
}

//其他关系仿函数用法一样

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

输出结果:

1 3 4 2 

4 3 2 1 

逻辑仿函数:

      功能描述:

        实现逻辑运算 实际开发中 运用的很少

      函数原型:

        template bool logical_and  //逻辑与

        template bool logical_or  //逻辑或

        template bool logical_not  //逻辑非

#include 
using namespace std;
#include 
#include 
#include 

void printVec(vector &v){
    for(vector::iterator it = v.begin();it != v.end();it++){
        cout << *it << " ";
    }
    cout << endl;
}
void test01(){
    vectorv;
    
    v.push_back(true);
    v.push_back(false);
    v.push_back(false);
    v.push_back(true);
    
    //true 和 false 本质上是1 和 0
    printVec(v);
    
    vectorv2;
    
    //将v2的空间扩展到和v一样的大小
    v2.resize(v.size());
    
    //利用逻辑非 将容器v搬运到 容器v2中 并执行取反操作
    //参数解析:把v的区间begin到end 拷贝到从v2的begin从开始的地方
    //并执行 logical_not
    transform(v.begin(),v.end(),v2.begin(),logical_not());
    
    printVec(v2);
}

//其他关系仿函数用法一样

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

 输出结果:

1 0 0 1 

0 1 1 0  

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1015872.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号