int MyMinFunc(int a, int b)
{
return a < b ? a : b;
}
int MyMaxFunc(int a, int b)
{
return a > b ? a : b;
}
template<typename T>
class RefList
{
struct Node
{
T &t;
Node *n;
Node() = delete;
Node(T &t) : t(t), n(0) {}
Node(const Node &) = delete;
Node &operator=(const Node &) = delete;
~Node(){}
} *h;
public:
RefList() : h(0) {}
RefList(const RefList &) = delete;
RefList &operator=(const RefList &) = delete;
void AddLast(T &t)
{
if(!h) h = new Node(t);
else
{
Node *n = h;
while(n->n) n = n->n;
n->n = new Node(t);
}
}
T &operator[](unsigned i)
{
if(!i) return h->t;
Node *n = h;
while(i--) n = n->n;
return n->t;
}
~RefList()
{
while(h)
{
Node *n = h;
h = h->n;
delete n;
}
}
};
#include <iostream>
int main()
{
RefList<int (int, int)> MyArray;
MyArray.AddLast(MyMinFunc);
MyArray.AddLast(MyMaxFunc);
std::cout << MyArray[0](4, 7) << std::endl;
std::cout << MyArray[1](4, 7) << std::endl;
}
aW50IE15TWluRnVuYyhpbnQgYSwgaW50IGIpCnsKICAgIHJldHVybiBhIDwgYiA/IGEgOiBiOwp9CmludCBNeU1heEZ1bmMoaW50IGEsIGludCBiKQp7CiAgICByZXR1cm4gYSA+IGIgPyBhIDogYjsKfQoKdGVtcGxhdGU8dHlwZW5hbWUgVD4KY2xhc3MgUmVmTGlzdAp7CiAgICBzdHJ1Y3QgTm9kZQogICAgewogICAgICAgIFQgJnQ7CiAgICAgICAgTm9kZSAqbjsKICAgICAgICBOb2RlKCkgPSBkZWxldGU7CiAgICAgICAgTm9kZShUICZ0KSA6IHQodCksIG4oMCkge30KICAgICAgICBOb2RlKGNvbnN0IE5vZGUgJikgPSBkZWxldGU7CiAgICAgICAgTm9kZSAmb3BlcmF0b3I9KGNvbnN0IE5vZGUgJikgPSBkZWxldGU7CiAgICAgICAgfk5vZGUoKXt9CiAgICB9ICpoOwpwdWJsaWM6CiAgICBSZWZMaXN0KCkgOiBoKDApIHt9CiAgICBSZWZMaXN0KGNvbnN0IFJlZkxpc3QgJikgPSBkZWxldGU7CiAgICBSZWZMaXN0ICZvcGVyYXRvcj0oY29uc3QgUmVmTGlzdCAmKSA9IGRlbGV0ZTsKICAgIHZvaWQgQWRkTGFzdChUICZ0KQogICAgewogICAgICAgIGlmKCFoKSBoID0gbmV3IE5vZGUodCk7CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgTm9kZSAqbiA9IGg7CiAgICAgICAgICAgIHdoaWxlKG4tPm4pIG4gPSBuLT5uOwogICAgICAgICAgICBuLT5uID0gbmV3IE5vZGUodCk7CiAgICAgICAgfQogICAgfQogICAgVCAmb3BlcmF0b3JbXSh1bnNpZ25lZCBpKQogICAgewogICAgICAgIGlmKCFpKSByZXR1cm4gaC0+dDsKICAgICAgICBOb2RlICpuID0gaDsKICAgICAgICB3aGlsZShpLS0pIG4gPSBuLT5uOwogICAgICAgIHJldHVybiBuLT50OwogICAgfQogICAgflJlZkxpc3QoKQogICAgewogICAgICAgIHdoaWxlKGgpCiAgICAgICAgewogICAgICAgICAgICBOb2RlICpuID0gaDsKICAgICAgICAgICAgaCA9IGgtPm47CiAgICAgICAgICAgIGRlbGV0ZSBuOwogICAgICAgIH0KICAgIH0KfTsKCiNpbmNsdWRlIDxpb3N0cmVhbT4KCmludCBtYWluKCkKewogICAgUmVmTGlzdDxpbnQgKGludCwgaW50KT4gTXlBcnJheTsKICAgIE15QXJyYXkuQWRkTGFzdChNeU1pbkZ1bmMpOwogICAgTXlBcnJheS5BZGRMYXN0KE15TWF4RnVuYyk7CiAgICBzdGQ6OmNvdXQgPDwgTXlBcnJheVswXSg0LCA3KSA8PCBzdGQ6OmVuZGw7CiAgICBzdGQ6OmNvdXQgPDwgTXlBcnJheVsxXSg0LCA3KSA8PCBzdGQ6OmVuZGw7Cn0=