标签:out putchar padding 组合 put this template new 静态
1
2
3
4
5
6
7
8
9
|
//less的定义 template < typename _Tp> struct less : public binary_function<_Tp, _Tp, bool > { bool operator()( const _Tp& __x, const _Tp& __y) const { return __x < __y; } }; |
1
2
3
4
|
//set的定义 template < typename _Key, typename _Compare = std::less<_Key>, typename _Alloc = std::allocator<_Key> > class set; |
#include <stdlib.h> /* Callback function */ int compare_ints_function(void*A,void*B) { return*((int*)(A))<*((int*)(B)); } /* Declaration of C sorting function */ void sort(void*first_item,size_t item_size,void*last_item,int(*cmpfunc)(void*,void*)); int main(void) { int items[]={4,3,1,2}; sort((void*)(items),sizeof(int),(void*)(items +3), compare_ints_function); return 0; }
class compare_class{ public: bool operator()(int A, int B)const{return A < B;} }; // Declaration of C++ sorting function. template<class ComparisonFunctor> void sort_ints(int* begin_items, int num_items, ComparisonFunctor c); int main(){ int items[]={4, 3, 1, 2}; compare_class functor; sort_ints(items, sizeof(items)/sizeof(items[0]), functor); }
List<String> list =Arrays.asList("10", "1", "20", "11", "21", "12"); Comparator<String> numStringComparator =new Comparator<String>(){ publicint compare(String o1, String o2){ returnInteger.valueOf(o1).compareTo(Integer.valueOf(o2)); } }; Collections.sort(list, numStringComparator);
#include <iostream> #include <algorithm> #include <cstdio> #include <ctime> #include <cstring> #include <string> #include <set> typedef long long LL; class Prt{ char c[53]; int top; public: template <class T> Prt& operator()(T x); Prt& operator()(LL x); Prt& operator()(int x); Prt& operator()(char x); Prt& operator()(const char*x); Prt& operator()(double x); Prt& operator()(const std::string x); Prt& operator()(double x,int k){ sprintf(c,"%%.%dlf",k); printf(c,x); return *this; } }; template <typename T> Prt& Prt::operator()(T x){ std::cout<<x; return *this; } Prt& Prt::operator()(LL x){ if(x==0){ putchar(‘0‘); return *this; } if(x<0){ putchar(‘-‘); x=-x; } top=0; while(x>0){ c[top++]=‘0‘+x%10; x/=10; } while(top--){ putchar(c[top]); } return *this; } Prt& Prt::operator()(int x){ return operator()((LL)(x)); } Prt& Prt::operator()(char x){ putchar(x); return *this; } Prt& Prt::operator()(const char*x){ printf("%s",x); return *this; } Prt& Prt::operator()(double x){ printf("%lf",x); return *this; } Prt& Prt::operator()(const std::string x){ return operator()(x.data()); } Prt prt; struct st{int x,y;st(){x=y=0;}st(int a,int b){x=a;y=b;}}; std::ostream& operator<<(std::ostream& fout,const st&x){ fout<<"["<<x.x<<","<<x.y<<"]"; return fout; } int main(){ prt(">>> prt(\"LL:\")(12341231234123ll)(\‘ \‘)(\"int:\")(15)(\‘\\n\‘);\n"); prt("LL:")(12341231234123ll)(‘ ‘)("int:")(15)(‘\n‘); prt(‘\n‘); prt(">>> prt(\"char:\")(\‘a\‘)(\" char*(/string):\")(std::string(\"abc\"))(\" d \")\ ((std::string(\"abc\")).data())(\‘\\n\‘);\n"); prt("char:")(‘a‘)(" char*(/string):")(std::string("abc"))(" d ") ((std::string("abc")).data())(‘\n‘); prt(‘\n‘); prt(">>> prt(\"double:\")(1.5)(\" double[int]:\")(10.12349746192736,4)(\‘\\n\‘);\n"); prt("double:")(1.5)(" double[int]:")(10.12349746192736,4)(‘\n‘); prt("\n>>> prt(st(12,31));\n"); prt(st(12,31)); prt(‘\n‘); return 0; }
标签:out putchar padding 组合 put this template new 静态
原文地址:https://www.cnblogs.com/xietianjiao/p/12344086.html