发现C++不是一般的强大……
谁知道这个程序做了什么,并且是怎么做到的?顺便问问,有没有比较简单的方法做到这种效果?
[it]改成了比较一般化的变量名,现在应该很容易看出功能了- -[/it]
/*****************************************************************
** HighlightCodeV3.0 software by yzfy(雨中飞燕) http:// **
*****************************************************************/
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
struct stu
{
string name;
int data[4];
static bool comp(const stu& s1,const stu& s2,int i)
{
return s1.data[i] < s2.data[i];
}
void print()
{
printf("%s %d %d %d %d\n",name.c_str(),
data[0],data[1],data[2],data[3]);
}
};
template<class T,class third_t,class ret_t>
struct binder3rd : binary_function<T,T,ret_t>
{
protected:
ret_t (*m_func)(const T&,const T&,third_t);
third_t m_third;
public:
binder3rd(ret_t (*func)(const T&,const T&,third_t),third_t third)
:m_func(func),m_third(third){}
ret_t operator()(const T& s1,const T& s2)
{
return m_func(s1,s2,m_third);
}
};
template<class T,class third_t,class ret_t>
binder3rd<T,third_t,ret_t> bind3rd(ret_t (*func)(const T&,const T&,third_t),third_t i)
{
return binder3rd<T,third_t,ret_t>(func,i);
}
vector<stu> t;
int main()
{
stu s[4]=
{
{"aaa",1,2,3,4},
{"bbb",3,4,2,1},
{"ccc",2,3,4,2},
{"ddd",4,1,1,3}
};
for_each(s,s+4,mem_fun_ref(&stu::print));
putchar('\n');
for (int i=0;i<4;i++)
{
sort(s,s+4,bind3rd(&stu::comp,i));
for_each(s,s+4,mem_fun_ref(&stu::print));
putchar('\n');
}
}
** HighlightCodeV3.0 software by yzfy(雨中飞燕) http:// **
*****************************************************************/
#include <iostream>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
struct stu
{
string name;
int data[4];
static bool comp(const stu& s1,const stu& s2,int i)
{
return s1.data[i] < s2.data[i];
}
void print()
{
printf("%s %d %d %d %d\n",name.c_str(),
data[0],data[1],data[2],data[3]);
}
};
template<class T,class third_t,class ret_t>
struct binder3rd : binary_function<T,T,ret_t>
{
protected:
ret_t (*m_func)(const T&,const T&,third_t);
third_t m_third;
public:
binder3rd(ret_t (*func)(const T&,const T&,third_t),third_t third)
:m_func(func),m_third(third){}
ret_t operator()(const T& s1,const T& s2)
{
return m_func(s1,s2,m_third);
}
};
template<class T,class third_t,class ret_t>
binder3rd<T,third_t,ret_t> bind3rd(ret_t (*func)(const T&,const T&,third_t),third_t i)
{
return binder3rd<T,third_t,ret_t>(func,i);
}
vector<stu> t;
int main()
{
stu s[4]=
{
{"aaa",1,2,3,4},
{"bbb",3,4,2,1},
{"ccc",2,3,4,2},
{"ddd",4,1,1,3}
};
for_each(s,s+4,mem_fun_ref(&stu::print));
putchar('\n');
for (int i=0;i<4;i++)
{
sort(s,s+4,bind3rd(&stu::comp,i));
for_each(s,s+4,mem_fun_ref(&stu::print));
putchar('\n');
}
}
[[it] 本帖最后由 StarWing83 于 2008-6-24 09:45 编辑 [/it]]