#pragma
warning(disable: 4786)
#include < vector >
#include < set >
#include < map >
#include < iostream >
#include < string >
#include < algorithm >
#include < functional >
using namespace std;
struct gtstr
... {
bool operator()(const char * s1, const char * s2) const
...{
return (strcmp(s1, s2) > 0);
}
} ;
class person
... {
public:
person(const string &name,const int & age ):m_name(name),m_age(age)
...{
};
friend ostream operator<<(ostream & out,const person & m_person)
...{
out<<m_person.m_name<<' ';
out<<m_person.m_age;
return out;
}
int putprint()
...{
cout<<m_name<<' ';
cout<<m_age<<endl;
return 0 ;
}
public:
string m_name;//名字
int m_age;//年龄
} ;
class agesort: public binary_function < person,person, bool >
... {//按年龄排序
public:
bool operator()(const person& person1,const person& person2)
...{
return person1.m_age>person2.m_age;
};
} ;
class personprint: public unary_function < person, void >
... {
public :
void operator()(const person& person1)
...{
cout<<person1.m_name<<' ';
cout<<person1.m_age<<endl;
}
} ;
int main()
... {
vector<person> personvector;
personvector.push_back(person("lili",12));
personvector.push_back(person("lucy",23));
copy(personvector.begin(),personvector.end(),ostream_iterator<person>(cout," "));
agesort agesort1;
sort(personvector.begin(),personvector.end(),agesort1);
personprint personprint1;
for_each(personvector.begin(),personvector.end(),personprint1);
for_each(personvector.begin(),personvector.end(),mem_fun_ref(&person::putprint));
// copy(personvector.begin(),personvector.end(),ostream_iterator<person>(cout," "));
return 0;
}
#include < vector >
#include < set >
#include < map >
#include < iostream >
#include < string >
#include < algorithm >
#include < functional >
using namespace std;
struct gtstr
... {
bool operator()(const char * s1, const char * s2) const
...{
return (strcmp(s1, s2) > 0);
}
} ;
class person
... {
public:
person(const string &name,const int & age ):m_name(name),m_age(age)
...{
};
friend ostream operator<<(ostream & out,const person & m_person)
...{
out<<m_person.m_name<<' ';
out<<m_person.m_age;
return out;
}
int putprint()
...{
cout<<m_name<<' ';
cout<<m_age<<endl;
return 0 ;
}
public:
string m_name;//名字
int m_age;//年龄
} ;
class agesort: public binary_function < person,person, bool >
... {//按年龄排序
public:
bool operator()(const person& person1,const person& person2)
...{
return person1.m_age>person2.m_age;
};
} ;
class personprint: public unary_function < person, void >
... {
public :
void operator()(const person& person1)
...{
cout<<person1.m_name<<' ';
cout<<person1.m_age<<endl;
}
} ;
int main()
... {
vector<person> personvector;
personvector.push_back(person("lili",12));
personvector.push_back(person("lucy",23));
copy(personvector.begin(),personvector.end(),ostream_iterator<person>(cout," "));
agesort agesort1;
sort(personvector.begin(),personvector.end(),agesort1);
personprint personprint1;
for_each(personvector.begin(),personvector.end(),personprint1);
for_each(personvector.begin(),personvector.end(),mem_fun_ref(&person::putprint));
// copy(personvector.begin(),personvector.end(),ostream_iterator<person>(cout," "));
return 0;
}
释放空间
#include
<
string
>
#include < vector >
#include < iostream >
#include < algorithm >
#include < functional >
#include < map >
using namespace std;
class name
... {
public:
name()...{
++count;
cout<<"construct"<<count<<' ';
};
~name()
...{
cout<<"~construct"<<count<<' ';
--count;
}
static int count;
} ;
template < typename classtype >
class classdelete
... {
public:
operator()(classtype *p)
...{
if (p)
...{
delete p;
}
}
} ;
template < typename classtype,typename deletetype >
class mapseconddelete
... {
public:
operator()(classtype maptype)
...{
if (maptype.second)
...{
deletetype m_delete;
m_delete(maptype.second);
}
}
} ;
int name::count = 0 ;
int main()
... {
/**//*
vector<name*> m_namelist;
for (int i=0;i<10;++i)
{
name *p=new name;
m_namelist.push_back(p);
}
classdelete<name> m_classdelete;
std::for_each(m_namelist.begin(),m_namelist.end(),m_classdelete);
*/
map<int,name*> m_namemap;
typedef map<int,name*> maptype ;
for (int j=0;j<10;++j)
...{
name *p =new name;
m_namemap.insert(make_pair(j,p));
}
mapseconddelete<maptype::value_type,classdelete<name> > m_mapseconddelete;
std::for_each(m_namemap.begin(),m_namemap.end(),m_mapseconddelete);
return 0;
}
#include < vector >
#include < iostream >
#include < algorithm >
#include < functional >
#include < map >
using namespace std;
class name
... {
public:
name()...{
++count;
cout<<"construct"<<count<<' ';
};
~name()
...{
cout<<"~construct"<<count<<' ';
--count;
}
static int count;
} ;
template < typename classtype >
class classdelete
... {
public:
operator()(classtype *p)
...{
if (p)
...{
delete p;
}
}
} ;
template < typename classtype,typename deletetype >
class mapseconddelete
... {
public:
operator()(classtype maptype)
...{
if (maptype.second)
...{
deletetype m_delete;
m_delete(maptype.second);
}
}
} ;
int name::count = 0 ;
int main()
... {
/**//*
vector<name*> m_namelist;
for (int i=0;i<10;++i)
{
name *p=new name;
m_namelist.push_back(p);
}
classdelete<name> m_classdelete;
std::for_each(m_namelist.begin(),m_namelist.end(),m_classdelete);
*/
map<int,name*> m_namemap;
typedef map<int,name*> maptype ;
for (int j=0;j<10;++j)
...{
name *p =new name;
m_namemap.insert(make_pair(j,p));
}
mapseconddelete<maptype::value_type,classdelete<name> > m_mapseconddelete;
std::for_each(m_namemap.begin(),m_namemap.end(),m_mapseconddelete);
return 0;
}