1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <iostream> #include <algorithm> #include <iterator> #include <boost/bind.hpp> struct S { bool ascending; bool Compare(int lhs, int rhs) { return ascending ? (lhs < rhs) : (rhs < lhs); } }; int main () { int i[] = { 1, 3, 5, 7, 8, 6, 4, 2 }; S s; s.ascending = true; std::sort(i, i+8, boost::bind(S::Compare, &s, _1, _2)); std::copy(i, i+8, std::ostream_iterator<int>(std::cout, " ")); std::cout << "\n"; s.ascending = false; std::sort(i, i+8, boost::bind(S::Compare, &s, _1, _2)); std::copy(i, i+8, std::ostream_iterator<int>(std::cout, " ")); std::cout << "\n"; } |
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8aXRlcmF0b3I+CiNpbmNsdWRlIDxib29zdC9iaW5kLmhwcD4KCnN0cnVjdCBTIHsKICBib29sIGFzY2VuZGluZzsKICBib29sIENvbXBhcmUoaW50IGxocywgaW50IHJocykgewogICAgcmV0dXJuIGFzY2VuZGluZyA/IChsaHMgPCByaHMpIDogKHJocyA8IGxocyk7CiAgfQp9OwoKaW50IG1haW4gKCkgewoKICBpbnQgaVtdID0geyAxLCAzLCA1LCA3LCA4LCA2LCA0LCAyIH07CiAgUyBzOwogIHMuYXNjZW5kaW5nID0gdHJ1ZTsKICBzdGQ6OnNvcnQoaSwgaSs4LCBib29zdDo6YmluZChTOjpDb21wYXJlLCAmcywgXzEsIF8yKSk7CiAgc3RkOjpjb3B5KGksIGkrOCwgc3RkOjpvc3RyZWFtX2l0ZXJhdG9yPGludD4oc3RkOjpjb3V0LCAiICIpKTsKICBzdGQ6OmNvdXQgPDwgIlxuIjsKCiAgcy5hc2NlbmRpbmcgPSBmYWxzZTsKICBzdGQ6OnNvcnQoaSwgaSs4LCBib29zdDo6YmluZChTOjpDb21wYXJlLCAmcywgXzEsIF8yKSk7CiAgc3RkOjpjb3B5KGksIGkrOCwgc3RkOjpvc3RyZWFtX2l0ZXJhdG9yPGludD4oc3RkOjpjb3V0LCAiICIpKTsKICBzdGQ6OmNvdXQgPDwgIlxuIjsKfQ==
prog.cpp: In function ‘int main()’: prog.cpp:18: error: invalid use of non-static member function ‘bool S::Compare(int, int)’ prog.cpp:23: error: invalid use of non-static member function ‘bool S::Compare(int, int)’
-
result: Compilation error (maybe you wish to see an example for C++ 4.7.2)


