题目网址:http://cxsjsxmooc.openjudge.cn/2018t3springw8/
【1:goodcopy】
注意从后往前复制即可
1 template <class T> 2 struct GoodCopy { 3 public: 4 void operator() (T* first,T* last,T* second){ 5 T* tmp=last-1; 6 second=second-first+last-1; 7 for(;tmp!=first-1;tmp--,second--){ 8 *second=*tmp; 9 } 10 } 11 };
【2:按距离排序】
按题目要求进行条件判断即可
1 template <class T1,class T2> 2 struct Closer { 3 T1 n; 4 T2 op; 5 Closer(T1 value,T2 o):n(value),op(o){} 6 bool operator() (T1 v1,T1 v2){ 7 if(op(v1,n)!=op(v2,n)) 8 return op(v1,n)<op(v2,n); 9 else{ 10 return v1<v2; 11 } 12 } 13 };
【3:很难蒙混过关的CArray3d三维数组模板类】
提示
1. a[i][j][k] 这个表达式的第一个[]返回一个内部类的对象,该内部类也重载了[],且返回值为指针。
2. 必要时需重载对象到指针的强制类型转换运算符
1 class CArray3D 2 { 3 public: 4 class neibu{ 5 public: 6 T* array; 7 int b,c; 8 neibu(T* a,int bb,int cc){ 9 array=a;b=bb;c=cc; 10 } 11 T* operator[](int i){ 12 return array+i*c; 13 } 14 operator void* (){ 15 return (void*)array; 16 } 17 }; 18 T* array; 19 int a,b,c; 20 CArray3D(int aa,int bb,int cc){ 21 a=aa;b=bb;c=cc; 22 array=new T[a*b*c+1]; 23 } 24 neibu operator[](int i){ 25 return neibu(array+i*b*c,b,c); 26 } 27 28 };
【4:函数对象的过滤器】
注意类A里面只重载了<运算符
1 template <class T> 2 class FilterClass{ 3 public: 4 int m,n; 5 FilterClass(int mm,int nn):m(mm),n(nn){} 6 bool operator() (T v){ 7 if((T)m<v&&v<(T)n){ 8 return true; 9 } 10 else { 11 return false; 12 } 13 } 14 };
【5:白给的list排序】
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <list> 5 using namespace std; 6 int main() 7 { 8 double a[] = {1.2,3.4,9.8,7.3,2.6}; 9 list<double> lst(a,a+5); 10 lst.sort( 11 greater<double>() 12 ); 13 14 for(list<double>::iterator i = lst.begin(); i != lst.end(); ++i) 15 cout << * i << "," ; 16 return 0; 17 }
【6:我自己的 ostream_iterator】
1 template<class T> 2 class myostream_iteraotr 3 { 4 public: 5 ostream& o; 6 string str; 7 myostream_iteraotr(ostream& os,string s):o(os),str(s){ 8 } 9 myostream_iteraotr& operator *(){ 10 return *this; 11 } 12 void operator=(T value){ 13 o<<value<<str; 14 } 15 void operator++(){ 16 } 17 };
算式 | 效果 |
---|---|
ostream_iterator<T>(ostream) | 为ostream产生一个ostream迭代器 |
ostream_iterator<T>(ostream,delim) | 为ostream产生一个ostream迭代器,且各元素间以delim为分隔符(delim的型别是const char*) |
*iter | 无实际操作(传回iter) |
iter=value | 将value写到ostream |
++iter | 无实际意义(传回iter) |
iter++ | 无实际意义(传回iter) |
来源:https://blog.csdn.net/ilovetbl/article/details/51470510
【7:List】
1 #include <iostream> 2 #include <list> 3 #include <string> 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 cin>>n; 10 string command; 11 list< int > ls[20001]; 12 13 while(n--){ 14 cin>>command; 15 if(command=="new"){ 16 int id; 17 cin>>id; 18 } 19 else if(command=="add"){ 20 int id,value; 21 cin>>id>>value; 22 ls[id].push_back(value); 23 } 24 else if(command=="merge"){ 25 int id1,id2; 26 cin>>id1>>id2; 27 ls[id1].merge(ls[id2]); 28 } 29 else if(command=="unique"){ 30 int id; cin>>id; 31 ls[id].sort(); 32 ls[id].unique(); 33 } 34 else if(command=="out"){ 35 int id; cin>>id; 36 ls[id].sort(); 37 for(list<int>::iterator j=ls[id].begin();j!=ls[id].end();j++){ 38 cout<<*j<<" "; 39 } 40 cout<<endl; 41 } 42 } 43 return 0; 44 }