现在的位置: 首页 > 综合 > 正文

stl实现计数存对象,实现更高形式的抽象

2013年10月11日 ⁄ 综合 ⁄ 共 1057字 ⁄ 字号 评论关闭

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <functional>
using namespace std;
class fun{
private:
int a;
int b;
public:
 fun(int x=0,int y=0){a=x;b=y;}
 bool operator==(fun &a){if(this->a==a.a&&this->b==a.b)
 return true;
 else return false;
  }
  void show(){cout<<"a= "<<a<<"b= "<<b<<endl;}
};
bool equal_function(fun &a,fun &b)
{
    return a==b;

}
class equal_class_function
{
    private:
    fun a;
    public:
    equal_class_function(fun &b):a(b){}
    bool operator()(fun &b)
    {
        if(a==b)return true;
        else return false;
    }
};

int main()
{
    fun a(1,1),b(1,1),c(2,2);
    cout<<(a==b)<<endl;
    cout<<(b==c)<<endl;

    vector<fun> coll1,coll2;
    for(int i=0;i<3;++i)
    {
        coll1.push_back(a);
    }
    for(int i=0;i<3;++i)
    coll2.push_back(c);
    bool haha =equal(coll1.begin(),coll1.end(),
          coll2.begin(),equal_function);
          cout<<haha<<endl;
     cout<<"....................."<<endl;
    fun d(1,1);
    int count=count_if(coll1.begin(),coll1.end(),
                     equal_class_function(d));
    cout<<count<<endl;

    for_each(coll1.begin(),coll1.end(),mem_fun_ref(&fun::show));
    cout << "Hello world!" << endl;
    return 0;
}

抱歉!评论已关闭.