StarWing83 发布留言 2008-6-24 09:11
发现C++不是一般的强大……
谁知道这个程序做了什么,并且是怎么做到的?
顺便问问,有没有比较简单的方法做到这种效果?
改成了比较一般化的变量名,现在应该很容易看出功能了- -[/it]
/***************************************************************** ** HighlightCodeV3.0 software by yzfy(雨中飞燕) http://yzfy.org ** *****************************************************************/ #include #include #include #include #include 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 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'); } }
|
[ 本帖最后由 StarWing83 于 2008-6-24 09:45 编辑 [/it]]
yyow3193 发布留言 2008-6-24 09:22
发现LZ不是一般的强
StarWing83 发布留言 2008-6-24 09:26
虽然代码长了点,但是的确做到了动态生成函数的功能……
如果能直接支持变量做模板参数就好了,不需要自己写绑定代码了……
mqh21364 发布留言 2008-6-24 09:32
看不太懂。。。。。。
好多方法都不知道什么意思,顶了先!
sunkaidong 发布留言 2008-6-24 10:20
翅膀里面用了很多c++的概念。。继承,重构。。。慢慢来。。。我刚也对运算符()看了好一会。。
中学者 发布留言 2008-6-24 10:58
靠·一堆STL,没看过。。 - -
StarWing83 发布留言 2008-6-24 10:59
事实上,里面只有三个地方是STL:for_each,sort和vector……
中学者 发布留言 2008-6-24 11:02
你继承那个不是标准模板?
StarWing83 发布留言 2008-6-24 11:24
那个也算么……其实继承不继承都无所谓,那个东西内容是空的……
我只是说明,那是个函数才继承的……
你看不惯可以去掉那个继承,运行也正常的- -
かわい 发布留言 2008-6-24 11:45
Orz
<

>
かわい 发布留言 2008-6-24 11:51
突然发现论坛帖子个人信息栏多了一项
不知道静老大为什么加上这一项呢
<

>
中学者 发布留言 2008-6-24 12:07
回复 9# StarWing83 的帖子
很强大,最近在学STL?
StarWing83 发布留言 2008-6-24 12:17
学过了,昨晚想那个作业,不想太麻烦写四个比较函数,然后就想起来函数模板和函数对象了,正好玩一玩……
cdj_cjf 发布留言 2008-7-19 21:09
C/C++
更多信息请参考C/C++
C/C++
http://bbs.palmjob.net/technology/1005/C/C++
http://bbs.palmjob.net/页: [1]