62 CPP的STL算法-for_each

模仿for_each自己写一个

#include <iostream>
#include <vector>
#include <forward_list>
using namespace std;
template <typename T>
void _foreach(const T first, const T last)
{
    for (auto it = first; it != last; it++)
        cout << *it << " ";
    cout << endl;
}

void zsshow(const string &no)
{
    cout << no << " ";
}

//用户自定义函数指针
template <typename T>
void _foreach2(const T first, const T last, void (*pfunc)(const string &))
{
    for (auto it = first; it != last; it++)
        pfunc(*it);
    cout << endl;
}

//调用类的成员函数
class CTmp
{
public:
    void show(const string &no)
    {
        cout << "call show:" << no << " ";
    }
    void operator()(const string &no)
    {
        cout << "call operator:" << no << " ";
    }
};
template <typename T>
void _foreach3(const T first, const T last, CTmp ct)
{
    for (auto it = first; it != last; it++)
        ct.show(*it);
    cout << endl;
}

template <typename T>
void _foreach4(const T first, const T last, CTmp ct)
{
    for (auto it = first; it != last; it++)
        ct(*it);
    cout << endl;
}

//模板

template <typename T1, typename T2>
void _foreach5(const T1 first, const T1 last, T2 func)
{
    for (auto it = first; it != last; it++)
        func(*it);
    cout << endl;
}
void test()
{
    // vector<int> bh{2, 3, 4, 5, 6};
    vector<string> bh{"2", "3", "4", "5", "6"};
    _foreach(bh.begin(), bh.end());
    //用户自定义函数指针
    _foreach2(bh.begin(), bh.end(), zsshow);
    //调用类的成员函数
    _foreach3(bh.begin(), bh.end(), CTmp());
    //仿函数
    _foreach4(bh.begin(), bh.end(), CTmp());
    //不管形参是什么数据类型,只要函数里面的代码相同,就可以整成模板

    cout << endl;
    //第三个参数既可以是函数,也可以是类,重载了小括号的类,也叫仿函数
    _foreach5(bh.begin(), bh.end(), CTmp());
    _foreach5(bh.begin(), bh.end(), zsshow);
}
int main()
{
    test();
    return 0;
}
/* 2 3 4 5 6 
2 3 4 5 6
call show:2 call show:3 call show:4 call show:5 call show:6
call operator:2 call operator:3 call operator:4 call operator:5 call operator:6

call operator:2 call operator:3 call operator:4 call operator:5 call operator:6 
2 3 4 5 6 */
#include <iostream>
#include <vector>
using namespace std;

template <typename T>
void show(const T &str)
{
    cout << str << endl;
}

template <class T>
class CTmp
{
public:
    void operator()(const T &no)
    {
        cout << "call operator:" << no << " ";
    }
};

template <typename T1, typename T2>
void _foreach(const T1 first, const T1 last, T2 func)
{
    for (auto it = first; it != last; it++)
        func(*it);
    cout << endl;
}
void test()
{
    // vector<int> bh{2, 3, 4, 5, 6};
    vector<string> bh{"A2", "A3", "A4", "A5", "A6"};
    _foreach(bh.begin(), bh.end(), CTmp<string>());
    _foreach(bh.begin(), bh.end(), show<string>);
}
int main()
{
    test();
    return 0;
}
/*
call operator:A2 call operator:A3 call operator:A4 call operator:A5 call operator:A6
A2
A3
A4
A5
A6*/

使用C++自带的for_each

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

template <typename T>
void show(const T &str)
{
    cout << str << endl;
}

template <class T>
class CTmp
{
public:
    void operator()(const T &no)
    {
        cout << "call operator:" << no << " ";
    }
};

void test()
{
    // vector<int> bh{2, 3, 4, 5, 6};
    vector<string> bh{"A2", "A3", "A4", "A5", "A6"};
    for_each(bh.begin(), bh.end(), CTmp<string>());
    for_each(bh.begin(), bh.end(), show<string>);
}
int main()
{
    test();
    return 0;
}
/*
call operator:A2 call operator:A3 call operator:A4 call operator:A5 call operator:A6
A2
A3
A4
A5
A6*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值