大挪耗

c++STL算法for_each

#include "iostream"
#include "vector"
#include "algorithm"
#include "list"
using namespace std;

void foreach(const vector <int>&v)
{
    for (auto it = v.begin(); it !=v.end() ; it++) {
        cout<<"亲爱的"<< *it<<"号:我是一只小小鸟。\n";
    }
}

template<typename T>

void zsshow(const T &no)
{
    cout<<"亲爱的"<<no<<"号:我是一只傻傻鸟。\n";

}

template<typename T>
class czs
{
public:
    void operator()(const T& no)
    {
        cout<<"亲爱的"<<no<<"号:我是一只傻傻鸟。\n";
    }
};

//void foreach(const vector<int>::iterator first,const vector <int>::iterator last)
//{
//    for (auto it= first;  it!=last ; it++) {
//        cout<<"亲爱的"<<*it<<"号:我是一只傻傻鸟。\n";
//
//    }
//}
//void foreach(const vector<string>::iterator first,const vector <string>::iterator last)
//{
//    for (auto it= first;  it!=last ; it++) {
//        cout<<"亲爱的"<<*it<<"号:我是一只傻傻鸟。\n";
//    }
//}

template <typename T1,typename T2>
void foreach(const T1 first,const T1 last,T2 pfun)
{
    for (auto it= first;  it!=last ; it++) {
       // cout<<"亲爱的"<<*it<<"号:我是一只傻傻鸟。\n";
       pfun(*it);
    }
}

template <typename T>
void foreach(const T first,const T last,void (*pfun)(const string&))
{
    for (auto it= first;  it!=last ; it++) {
        // cout<<"亲爱的"<<*it<<"号:我是一只傻傻鸟。\n";
        pfun(*it);

    }
}


template <typename T>
void foreach(const T first,const T last,T zs)
{
    for (auto it= first;  it!=last ; it++) {
        // cout<<"亲爱的"<<*it<<"号:我是一只傻傻鸟。\n";
        zs.show(*it);
    }
}

int main()
{
    //vector<int> bh={5,8,2,6,9,3,1,7};//存在超女号的容器。
    //vector<string>bh={"05","08","02","06","09","03","01","07"};//存放超女编号的容器。
    list<string>bh={"05","08","02","06","09","03","01","07"};//存放超女编号的容器。
    //写一个函数,在函数遍历容器,向超女表白表白的方法可定义。
    //foreach(bh.begin(),bh.end(),zsshow);// 不支持向部分超女表白。
    //czs zs;
    foreach(bh.begin(),bh.end(),zsshow<string>);
    //或者自己使用,要写头文件#include "algorithm"
    for_each(bh.begin(),bh.end(),czs<string>());

}

亲爱的05号:我是一只傻傻鸟。
亲爱的08号:我是一只傻傻鸟。
亲爱的02号:我是一只傻傻鸟。
亲爱的06号:我是一只傻傻鸟。
亲爱的09号:我是一只傻傻鸟。
亲爱的03号:我是一只傻傻鸟。
亲爱的01号:我是一只傻傻鸟。
亲爱的07号:我是一只傻傻鸟。
亲爱的05号:我是一只傻傻鸟。
亲爱的08号:我是一只傻傻鸟。
亲爱的02号:我是一只傻傻鸟。
亲爱的06号:我是一只傻傻鸟。
亲爱的09号:我是一只傻傻鸟。
亲爱的03号:我是一只傻傻鸟。
亲爱的01号:我是一只傻傻鸟。
亲爱的07号:我是一只傻傻鸟。


已发布

分类

可以收藏大挪耗一下。下载麻烦点城通网盘,站长保证下载速度,不会限速的,放心点就是了;分卷,安卓下载为txt:程序下载为url,不会下载参考不会下载。如果你想让本站活的久一点,请直接捐助

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注