18.04.04 作业STL 1
A:白给的list排序
描述
程序填空,产生指定输出
#include <cstdio> #include <iostream> #include <algorithm> #include <list> using namespace std; int main() { double a[] = {1.2,3.4,9.8,7.3,2.6}; list<double> lst(a,a+5); lst.sort( // 在此处补充你的代码 ); for(list<double>::iterator i = lst.begin(); i != lst.end(); ++i) cout << * i << "," ; return 0; }
输入
无
输出
9.8,7.3,3.4,2.6,1.2,
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 [](double a, double b) -> bool { return a > b; } 12 ); 13 14 for(list<double>::iterator i = lst.begin(); i != lst.end(); ++i) 15 cout << * i << "," ; 16 return 0; 17 }
B:按距离排序
描述
程序填空,输出指定结果
#include <iostream> #include <cmath> #include <algorithm> #include <string> using namespace std; template <class T1,class T2> struct Closer { // 在此处补充你的代码 }; int Distance1(int n1,int n2) { return abs(n1-n2); } int Distance2(const string & s1, const string & s2) { return abs((int)s1.length()- (int) s2.length()); } int a[10] = { 0,3,1,4,7,9,20,8,10,15}; string b[6] = {"American","Jack","To","Peking","abcdefghijklmnop","123456789"}; int main() { int n;string s; while( cin >> n >> s ) { sort(a,a+10,Closer<int ,int (*)(int ,int)> (n,Distance1)); for(int i = 0;i < 10; ++i) cout << a[i] << "," ; cout << endl; sort(b,b+6,Closer<string,int (*)(const string &,const string & )> (s,Distance2)); for(int i = 0;i < 6; ++i) cout << b[i] << "," ; cout << endl; } return 0; }
输入
多组数据,每组一行,是一个整数n和一个字符串s
输出
定义两个整数的距离为两个整数差的绝对值
定义两个字符串的距离为两个字符串长度差的绝对值
对每组数据:
对数组a按和n的距离从小到大排序后输出。距离相同的,值小的排在前面。
然后对数组b,按照和s的距离从小到大输出。距离相同的,字典序小的排在前面样例输入
2 a123456
4 a12345
样例输出
1,3,0,4,7,8,9,10,15,20,
American,Peking,123456789,Jack,To,abcdefghijklmnop,
4,3,1,7,0,8,9,10,15,20,
Peking,American,Jack,123456789,To,abcdefghijklmnop,
来源Guo Wei
1 #include <iostream> 2 #include <cmath> 3 #include <algorithm> 4 #include <string> 5 using namespace std; 6 template <class T1,class T2> 7 struct Closer { 8 T1 n; 9 T2 d; 10 Closer(T1 a, T2 d1) :n(a), d(d1) {} 11 bool operator()(const T1&a, const T1&b) { 12 if (d(a, n) == d(b,n)) 13 return a < b; 14 else return d(a,n)<d(b,n); 15 } 16 }; 17 18 int Distance1(int n1,int n2) { 19 return abs(n1-n2); 20 } 21 int Distance2(const string & s1, const string & s2) 22 { 23 return abs((int)s1.length()- (int) s2.length()); 24 } 25 int a[10] = { 0,3,1,4,7,9,20,8,10,15}; 26 string b[6] = {"American","Jack","To","Peking","abcdefghijklmnop","123456789"}; 27 int main() 28 { 29 int n;string s; 30 while( cin >> n >> s ) { 31 sort(a,a+10,Closer<int ,int (*)(int ,int)> (n,Distance1)); 32 for(int i = 0;i < 10; ++i) 33 cout << a[i] << "," ; 34 cout << endl; 35 sort(b,b+6,Closer<string,int (*)(const string &,const string & )> (s,Distance2)); 36 for(int i = 0;i < 6; ++i) 37 cout << b[i] << "," ; 38 cout << endl; 39 } 40 return 0; 41 }
C:goodcopy
描述
编写GoodCopy类模板,使得程序按指定方式输出
#include <iostream> using namespace std; template <class T> struct GoodCopy { // 在此处补充你的代码 }; int a[200]; int b[200]; string c[200]; string d[200]; template <class T> void Print(T s,T e) { for(; s != e; ++s) cout << * s << ","; cout << endl; } int main() { int t; cin >> t; while( t -- ) { int m ; cin >> m; for(int i = 0;i < m; ++i) cin >> a[i]; GoodCopy<int>()(a,a+m,b); Print(b,b+m); GoodCopy<int>()(a,a+m,a+m/2); Print(a+m/2,a+m/2 + m); for(int i = 0;i < m; ++i) cin >> c[i]; GoodCopy<string>()(c,c+m,d); Print(c,c+m); GoodCopy<string>()(c,c+m,c+m/2); Print(c+m/2,c+m/2 + m); } return 0; }
输入
第一行是整数 t,表示数据组数
每组数据:
第一行是整数 n , n < 50
第二行是 n 个整数
第三行是 n 个字符串
输出
将输入的整数原序输出两次,用","分隔
然后将输入的字符串原序输出两次,也用 ","分隔样例输入
2
4
1 2 3 4
Tom Jack Marry Peking
1
0
Ted
样例输出
1,2,3,4,
1,2,3,4,
Tom,Jack,Marry,Peking,
Tom,Jack,Marry,Peking,
0,
0,
Ted,
Ted,
来源Guo Wei
1 #include <iostream> 2 using namespace std; 3 4 5 template <class T> 6 struct GoodCopy { 7 void cpy(T*s,T*e,T*to) { 8 for (T*i = e-1; i >=s; i--) 9 *(to + (i - s)) = *i; 10 } 11 void operator()(T*s,T*e,T*to) { 12 cpy(s, e, to); 13 } 14 }; 15 16 int a[200]; 17 int b[200]; 18 string c[200]; 19 string d[200]; 20 21 template <class T> 22 void Print(T s,T e) { 23 for(; s != e; ++s) 24 cout << * s << ","; 25 cout << endl; 26 } 27 28 int main() 29 { 30 int t; 31 cin >> t; 32 while( t -- ) { 33 int m ; 34 cin >> m; 35 for(int i = 0;i < m; ++i) 36 cin >> a[i]; 37 GoodCopy<int>()(a,a+m,b); 38 Print(b,b+m); 39 GoodCopy<int>()(a,a+m,a+m/2); 40 Print(a+m/2,a+m/2 + m); 41 42 for(int i = 0;i < m; ++i) 43 cin >> c[i]; 44 GoodCopy<string>()(c,c+m,d); 45 Print(c,c+m); 46 GoodCopy<string>()(c,c+m,c+m/2); 47 Print(c+m/2,c+m/2 + m); 48 } 49 return 0; 50 }
D:函数对象的过滤器
描述
程序填空输出指定结果
#include <iostream> #include <vector> using namespace std; struct A { int v; A() { } A(int n):v(n) { }; bool operator<(const A & a) const { return v < a.v; } }; // 在此处补充你的代码 template <class T> void Print(T s,T e) { for(;s!=e; ++s) cout << *s << ","; cout << endl; } template <class T1, class T2,class T3> T2 Filter( T1 s,T1 e, T2 s2, T3 op) { for(;s != e; ++s) { if( op(*s)) { * s2 = * s; ++s2; } } return s2; } ostream & operator <<(ostream & o,A & a) { o << a.v; return o; } vector<int> ia; vector<A> aa; int main() { int m,n; while(cin >> m >> n) { ia.clear(); aa.clear(); int k,tmp; cin >> k; for(int i = 0;i < k; ++i) { cin >> tmp; ia.push_back(tmp); aa.push_back(tmp); } vector<int> ib(k); vector<A> ab(k); vector<int>::iterator p = Filter(ia.begin(),ia.end(),ib.begin(),FilterClass<int>(m,n)); Print(ib.begin(),p); vector<A>::iterator pp = Filter(aa.begin(),aa.end(),ab.begin(),FilterClass<A>(m,n)); Print(ab.begin(),pp); } return 0; }
输入
多组数据
每组数据两行
第一行是两个整数 m 和 n
第二行先是一个整数k ,然后后面跟着k个整数
输出
对每组数据,按原顺序输出第二行的后k个整数中,大于m且小于n的数
输出两遍
数据保证一定能找到符合要求的整数样例输入
1 3
1 2
2 8
5 1 2 3 4 9
样例输出
2,
2,
3,4,
3,4,
来源Guo Wei
1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 5 6 struct A { 7 int v; 8 A() { } 9 A(int n):v(n) { }; 10 bool operator<(const A & a) const { 11 return v < a.v; 12 } 13 }; 14 template<class T1> 15 struct FilterClass { 16 T1 m, n; 17 FilterClass(T1 a, T1 b):m(a), n(b) {} 18 bool operator()(T1 s) { 19 return (m < s) && (s < n); 20 } 21 }; 22 23 template <class T> 24 void Print(T s,T e) 25 { 26 for(;s!=e; ++s) 27 cout << *s << ","; 28 cout << endl; 29 } 30 template <class T1, class T2,class T3> 31 T2 Filter( T1 s,T1 e, T2 s2, T3 op) 32 { 33 for(;s != e; ++s) { 34 if( op(*s)) { 35 * s2 = * s; 36 ++s2; 37 } 38 } 39 return s2; 40 } 41 42 ostream & operator <<(ostream & o,A & a) 43 { 44 o << a.v; 45 return o; 46 } 47 vector<int> ia; 48 vector<A> aa; 49 int main() 50 { 51 int m,n; 52 while(cin >> m >> n) { 53 ia.clear(); 54 aa.clear(); 55 int k,tmp; 56 cin >> k; 57 for(int i = 0;i < k; ++i) { 58 cin >> tmp; 59 ia.push_back(tmp); 60 aa.push_back(tmp); 61 } 62 vector<int> ib(k); 63 vector<A> ab(k); 64 vector<int>::iterator p = Filter(ia.begin(),ia.end(),ib.begin(),FilterClass<int>(m,n)); 65 Print(ib.begin(),p); 66 vector<A>::iterator pp = Filter(aa.begin(),aa.end(),ab.begin(),FilterClass<A>(m,n)); 67 Print(ab.begin(),pp); 68 69 } 70 return 0; 71 }
E:很难蒙混过关的CArray3d三维数组模板类
描述
实现一个三维数组模版CArray3D,可以用来生成元素为任意类型变量的三维数组,输出指定结果
#include <iostream> #include <iomanip> #include <cstring> using namespace std; template <class T> class CArray3D { // 在此处补充你的代码 }; CArray3D<int> a(3,4,5); CArray3D<double> b(3,2,2); void PrintA() { for(int i = 0;i < 3; ++i) { cout << "layer " << i << ":" << endl; for(int j = 0; j < 4; ++j) { for(int k = 0; k < 5; ++k) cout << a[i][j][k] << "," ; cout << endl; } } } void PrintB() { for(int i = 0;i < 3; ++i) { cout << "layer " << i << ":" << endl; for(int j = 0; j < 2; ++j) { for(int k = 0; k < 2; ++k) cout << b[i][j][k] << "," ; cout << endl; } } } int main() { int No = 0; for( int i = 0; i < 3; ++ i ) { a[i]; for( int j = 0; j < 4; ++j ) { a[j][i]; for( int k = 0; k < 5; ++k ) a[i][j][k] = No ++; a[j][i][i]; } } PrintA(); memset(a[1],-1 ,20*sizeof(int)); memset(a[1],-1 ,20*sizeof(int)); PrintA(); memset(a[1][1],0 ,5*sizeof(int)); PrintA(); for( int i = 0; i < 3; ++ i ) for( int j = 0; j < 2; ++j ) for( int k = 0; k < 2; ++k ) b[i][j][k] = 10.0/(i+j+k+1); PrintB(); int n = a[0][1][2]; double f = b[0][1][1]; cout << "****" << endl; cout << n << "," << f << endl; return 0; }
样例输入
无
样例输出
layer 0:
0,1,2,3,4,
5,6,7,8,9,
10,11,12,13,14,
15,16,17,18,19,
layer 1:
20,21,22,23,24,
25,26,27,28,29,
30,31,32,33,34,
35,36,37,38,39,
layer 2:
40,41,42,43,44,
45,46,47,48,49,
50,51,52,53,54,
55,56,57,58,59,
layer 0:
0,1,2,3,4,
5,6,7,8,9,
10,11,12,13,14,
15,16,17,18,19,
layer 1:
-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,
layer 2:
40,41,42,43,44,
45,46,47,48,49,
50,51,52,53,54,
55,56,57,58,59,
layer 0:
0,1,2,3,4,
5,6,7,8,9,
10,11,12,13,14,
15,16,17,18,19,
layer 1:
-1,-1,-1,-1,-1,
0,0,0,0,0,
-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,
layer 2:
40,41,42,43,44,
45,46,47,48,49,
50,51,52,53,54,
55,56,57,58,59,
layer 0:
10,5,
5,3.33333,
layer 1:
5,3.33333,
3.33333,2.5,
layer 2:
3.33333,2.5,
2.5,2,
****
7,3.33333
提示建议做法:
1. a[i][j][k] 这个表达式的第一个[]返回一个内部类的对象,该内部类也重载了[],且返回值为指针。
2. 必要时需重载对象到指针的强制类型转换运算符
1 #include <iostream> 2 #include <iomanip> 3 #include <cstring> 4 using namespace std; 5 template <class T> 6 class CArray3D 7 { 8 public: 9 class CArray2D { 10 public: 11 int y, z; 12 T *p; 13 void set(int m, int n) { 14 y = m, z = n; 15 p = new T[m*n]; 16 } 17 T*operator[](int k) { 18 return p+k*z; 19 } 20 operator T*() { 21 return this->p; 22 } 23 }; 24 int x; 25 CArray2D *ptoa; 26 CArray3D(int m, int n, int k) :x(m) { 27 ptoa = new CArray2D[x]; 28 for (int i = 0; i < x; i++) 29 ptoa[i].set(n, k); 30 } 31 CArray2D&operator[](int k) { 32 return ptoa[k]; 33 } 34 }; 35 36 CArray3D<int> a(3,4,5); 37 CArray3D<double> b(3,2,2); 38 void PrintA() 39 { 40 for(int i = 0;i < 3; ++i) { 41 cout << "layer " << i << ":" << endl; 42 for(int j = 0; j < 4; ++j) { 43 for(int k = 0; k < 5; ++k) 44 cout << a[i][j][k] << "," ; 45 cout << endl; 46 } 47 } 48 } 49 void PrintB() 50 { 51 for(int i = 0;i < 3; ++i) { 52 cout << "layer " << i << ":" << endl; 53 for(int j = 0; j < 2; ++j) { 54 for(int k = 0; k < 2; ++k) 55 cout << b[i][j][k] << "," ; 56 cout << endl; 57 } 58 } 59 } 60 61 int main() 62 { 63 64 int No = 0; 65 for( int i = 0; i < 3; ++ i ) { 66 a[i]; 67 for( int j = 0; j < 4; ++j ) { 68 a[j][i]; 69 for( int k = 0; k < 5; ++k ) 70 a[i][j][k] = No ++; 71 a[j][i][i]; 72 } 73 } 74 PrintA(); 75 memset(a[1],-1 ,20*sizeof(int)); 76 memset(a[1],-1 ,20*sizeof(int)); 77 PrintA(); 78 memset(a[1][1],0 ,5*sizeof(int)); 79 PrintA(); 80 81 for( int i = 0; i < 3; ++ i ) 82 for( int j = 0; j < 2; ++j ) 83 for( int k = 0; k < 2; ++k ) 84 b[i][j][k] = 10.0/(i+j+k+1); 85 PrintB(); 86 int n = a[0][1][2]; 87 double f = b[0][1][1]; 88 cout << "****" << endl; 89 cout << n << "," << f << endl; 90 91 return 0; 92 }
内部类是这个意思的吗 还有这样的
F:我自己的 ostream_iterator
描述
程序填空输出指定结果
#include <iostream> #include <list> #include <string> using namespace std; template <class T1,class T2> void Copy(T1 s,T1 e, T2 x) { for(; s != e; ++s,++x) *x = *s; } template<class T> class myostream_iteraotr { // 在此处补充你的代码 }; int main() { const int SIZE = 5; int a[SIZE] = {5,21,14,2,3}; double b[SIZE] = { 1.4, 5.56,3.2,98.3,3.3}; list<int> lst(a,a+SIZE); myostream_iteraotr<int> output(cout,","); Copy( lst.begin(),lst.end(),output); cout << endl; myostream_iteraotr<double> output2(cout,"--"); Copy(b,b+SIZE,output2); return 0; }
输入
无
输出
5,21,14,2,3,
1.4--5.56--3.2--98.3--3.3--
1 #include <iostream> 2 #include <list> 3 #include <string> 4 using namespace std; 5 6 template <class T1,class T2> 7 void Copy(T1 s,T1 e, T2 x) 8 { 9 for(; s != e; ++s,++x) 10 *x = *s; 11 } 12 13 14 template<class T> 15 class myostream_iteraotr 16 { 17 public: 18 string sep; 19 ostream&os; 20 void operator++() {} 21 myostream_iteraotr(ostream&out, string s) :os(out), sep(s) {} 22 myostream_iteraotr&operator*() { 23 return *this; 24 } 25 myostream_iteraotr&operator=(T&s) { 26 os << s<<sep; 27 return *this; 28 } 29 }; 30 31 32 int main() 33 { const int SIZE = 5; 34 int a[SIZE] = {5,21,14,2,3}; 35 double b[SIZE] = { 1.4, 5.56,3.2,98.3,3.3}; 36 list<int> lst(a,a+SIZE); 37 myostream_iteraotr<int> output(cout,","); 38 Copy( lst.begin(),lst.end(),output); 39 cout << endl; 40 myostream_iteraotr<double> output2(cout,"--"); 41 Copy(b,b+SIZE,output2); 42 return 0; 43 }
注定失败的战争,也要拼尽全力去打赢它;
就算输,也要输得足够漂亮。