for_each

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

void output(int i)
{
        cout << "i= " << i << "\t";
}

class stroutput
{
        public:
         void operator()(int i )
        {

        cout << "i= " << i << "\t";
        }
};
struct mystr
{
        void operator()(int i)
        {
        cout << "i= " << i << "\t";
        }
}myobj;
bool great20(int i)
{
        if(i> 20)
        {
                return true;
        }
        return false;

}
int setnum()
{
        static int i = 0;
        return i++;
}
int main(int argc,char* argv[])
{
        vector<int> vec;
        vec.push_back(10);
        vec.push_back(20);
        vec.push_back(30);
        vec.push_back(40);
        vec.push_back(10);
        vec.push_back(20);
        vec.push_back(30);
        vec.push_back(40);
        vec.push_back(50);
        vector<int> vec2(vec.begin(),vec.end()--);
        //for_each
        //for_each(vec.begin(),vec.end(),output);
        //for_each(vec.begin(),vec.end(),stroutput());
        //for_each(vec.begin(),vec.end(),myobj);

        vector<int>::iterator it; 
        //find
        /*vector<int>::iterator it = find(vec.begin(),vec.end(),20);
        it++;
        cout << "*it = " << *it << endl;
        */

        //find_if
//      it = find_if(vec.begin(),vec.end(),great20);
//      cout << "*it = " << *it << endl;

        //find_first_of
        //int arr[] = {40,30};
        //it = find_first_of(vec.begin(),vec.end(),arr,arr+2);


        //count
        //int num = count(vec.begin(),vec.end(),10);
        //count_if
        int num = count_if(vec.begin(),vec.end(),great20);
        cout << "num = " << num << endl;
        //equal
        bool b = equal(vec.begin(),vec.end(),vec2.begin()+1);
        cout << "b=" << b << endl;
        //replace
        //replace(vec.begin(),vec.end(),20,100);

//      for_each(vec.begin(),vec.end(),output);
        vector<int> vec3(vec.size());
        replace_copy(vec.begin(),vec.end(),vec3.begin(),20,100);
//      for_each(vec3.begin(),vec3.end(),output);
        fill(vec3.begin(),vec3.end(),5);
        for_each(vec3.begin(),vec3.end(),output);
        vector<int> vec4(10);
        generate(vec4.begin(),vec4.end(),setnum);
        for_each(vec4.begin(),vec4.end(),output);
        return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值