update
This commit is contained in:
parent
2bc5789ae7
commit
1bd6f7e845
@ -62,4 +62,11 @@ add_executable(stacktp1 stl/stacktp1.cpp)
|
||||
add_executable(arraytp stl/arraytp.cpp)
|
||||
add_executable(tv friend/tv.cpp friend/use_tv.cpp friend/tvfm.h)
|
||||
add_executable(queuetp friend/queuetp.cpp)
|
||||
add_executable(error1 exception/error1.cpp)
|
||||
add_executable(error1 exception/error1.cpp)
|
||||
add_executable(pairs stl/pairs.cpp)
|
||||
add_executable(tempmemb stl/tempmemb.cpp)
|
||||
add_executable(tempmemb2 stl/tempmemb2.cpp)
|
||||
add_executable(tempparm stl/tempparm.cpp)
|
||||
add_executable(frnd2tmp stl/frnd2tmp.cpp)
|
||||
add_executable(tmp2tmp stl/tmp2tmp.cpp)
|
||||
add_executable(manyfmd stl/manyfmd.cpp)
|
60
base/stl/frnd2tmp.cpp
Normal file
60
base/stl/frnd2tmp.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/9/1.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* 模版的友元分3累
|
||||
* 非模版友元
|
||||
* 约束模版友元,即友元的类型取决于类被实例化时的类型
|
||||
* 非约束模版友元,即友元的所有具体化都是累的每一个具体化的友元。
|
||||
* @return
|
||||
*/
|
||||
template<typename T>
|
||||
class HasFriend {
|
||||
private:
|
||||
T item;
|
||||
static int ct;
|
||||
public:
|
||||
HasFriend(const T &i) : item(i) { ct++; }
|
||||
~HasFriend(){ct--;}
|
||||
friend void counts();
|
||||
friend void reports(HasFriend<T> &);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
int HasFriend<T>::ct = 0;
|
||||
|
||||
void counts(){
|
||||
cout <<"int count: "<<HasFriend<int>::ct << "; ";
|
||||
cout <<"double count: " <<HasFriend<double>::ct << endl;
|
||||
}
|
||||
|
||||
void reports(HasFriend<int> & hf){
|
||||
cout <<"HasFriend<int> : "<<hf.item << endl;
|
||||
}
|
||||
void reports(HasFriend<double> & hf){
|
||||
cout <<"HasFriend<double> : "<<hf.item << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
cout <<"No objects declared: ";
|
||||
counts();
|
||||
HasFriend<int> hfi1(10);
|
||||
cout <<"After hfi1 declared: ";
|
||||
counts();
|
||||
HasFriend<int> hfi2(20);
|
||||
cout <<"After hfi1 declared: ";
|
||||
counts();
|
||||
HasFriend<double> hfdb(10.5);
|
||||
cout <<"After hfdb declared: ";
|
||||
counts();
|
||||
reports(hfi1);
|
||||
reports(hfi2);
|
||||
reports(hfdb);
|
||||
return 0;
|
||||
}
|
||||
|
32
base/stl/manyfmd.cpp
Normal file
32
base/stl/manyfmd.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/9/1.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
template<typename T>
|
||||
class ManyFriend{
|
||||
private:
|
||||
T item;
|
||||
public:
|
||||
ManyFriend(const T & i) :item(i) {}
|
||||
|
||||
template<typename C,typename D> friend void show(C &,D &);
|
||||
};
|
||||
|
||||
template<typename C,typename D> void show(C & c ,D & d){
|
||||
cout<<c.item <<", " << d.item <<endl;
|
||||
}
|
||||
int main() {
|
||||
|
||||
ManyFriend<int> hfi1(10);
|
||||
ManyFriend<int> hfi2(20);
|
||||
ManyFriend<double> hfdb(10.5);
|
||||
cout <<"hfi1, hfi2: ";
|
||||
show(hfi1,hfi2);
|
||||
cout <<"hfdb, hfi2: ";
|
||||
show(hfdb,hfi2);
|
||||
return 0;
|
||||
}
|
||||
|
52
base/stl/pairs.cpp
Normal file
52
base/stl/pairs.cpp
Normal file
@ -0,0 +1,52 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/9/1.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
template<class T1,class T2>
|
||||
class Pair{
|
||||
private:
|
||||
T1 a;
|
||||
T2 b;
|
||||
public:
|
||||
T1 & first();
|
||||
T2 & second();
|
||||
T1 first() const{ return a;}
|
||||
T2 & second() const{ return b;}
|
||||
Pair(const T1 & aval,const T2 & bval):a(aval),b(bval){}
|
||||
Pair(){}
|
||||
};
|
||||
|
||||
template<class T1,class T2>
|
||||
T1 & Pair<T1,T2>::first() {
|
||||
return a;
|
||||
}
|
||||
|
||||
template<class T1,class T2>
|
||||
T2 & Pair<T1,T2>::second(){
|
||||
return b;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
Pair<string,int> ratings[4] {
|
||||
Pair<string,int>("The Purpled Duck",5),
|
||||
Pair<string,int>("Jaquie's Frisco Al Fresco",4),
|
||||
Pair<string,int>("Cafe Souffle",5),
|
||||
Pair<string,int>("Bertie's Eats",3)
|
||||
};
|
||||
int joints = sizeof (ratings) /sizeof (Pair<string,int>);
|
||||
cout <<"Rating:\t Batery\n";
|
||||
for (int i = 0; i < joints; ++i) {
|
||||
cout <<ratings[i].second() <<":\t" << ratings[i].first()<<endl;
|
||||
}
|
||||
cout <<"Oops! Revised rating:\n";
|
||||
ratings[3].first() = "Bertie's Fab Eats";
|
||||
ratings[3].second() = 6;
|
||||
cout << ratings[3].second() <<":\t" <<ratings[3].first() << endl;
|
||||
return 0;
|
||||
}
|
||||
|
56
base/stl/tempmemb.cpp
Normal file
56
base/stl/tempmemb.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/9/1.
|
||||
//
|
||||
/**
|
||||
* 成员模版
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//成员模版
|
||||
template<typename T>
|
||||
class beta {
|
||||
private:
|
||||
template<typename V>
|
||||
class hold {
|
||||
private:
|
||||
V val;
|
||||
public:
|
||||
hold(V v = 0) : val(v) {}
|
||||
|
||||
void show() const { cout << val << endl; }
|
||||
|
||||
V Value() const { return val; }
|
||||
};
|
||||
|
||||
hold<T> q;
|
||||
hold<int> n;
|
||||
public:
|
||||
beta(T t, int i) : q(t), n(i) {}
|
||||
|
||||
template<typename U>
|
||||
U blab(U u, T t) {
|
||||
return (n.Value() + q.Value()) * u / t;
|
||||
}
|
||||
|
||||
void Show() const {
|
||||
q.show();
|
||||
n.show();
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
|
||||
beta<double > guy(3.5,3);
|
||||
cout <<"T was set to double \n";
|
||||
guy.Show();
|
||||
cout <<"V was set to T,which is double ,then V was set to int \n";
|
||||
cout <<guy.blab(10,2.3)<<endl;
|
||||
cout <<"U was set to int\n";
|
||||
cout << guy.blab(10.0,2.3) << endl;
|
||||
cout <<"U was set to double\n";
|
||||
cout <<"Done\n";
|
||||
return 0;
|
||||
}
|
||||
|
64
base/stl/tempmemb2.cpp
Normal file
64
base/stl/tempmemb2.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/9/1.
|
||||
//
|
||||
/**
|
||||
* 成员模版
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//成员模版 在外部定义模版方法
|
||||
template<typename T>
|
||||
class beta {
|
||||
private:
|
||||
template<typename V>
|
||||
class hold;
|
||||
|
||||
hold<T> q;
|
||||
hold<int> n;
|
||||
public:
|
||||
beta(T t, int i) : q(t), n(i) {}
|
||||
|
||||
template<typename U>
|
||||
U blab(U u, T t);
|
||||
|
||||
void Show() const {
|
||||
q.show();
|
||||
n.show();
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
template<typename V>
|
||||
class beta<T>::hold {
|
||||
private:
|
||||
V val;
|
||||
public:
|
||||
hold(V v = 0) : val(v) {}
|
||||
|
||||
void show() const { cout << val << endl; }
|
||||
|
||||
V Value() const { return val; }
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
template<typename U>
|
||||
U beta<T>::blab(U u, T t) {
|
||||
return (n.Value() + q.Value()) * u / t;
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
beta<double> guy(3.5, 3);
|
||||
cout << "T was set to double \n";
|
||||
guy.Show();
|
||||
cout << "V was set to T,which is double ,then V was set to int \n";
|
||||
cout << guy.blab(10, 2.3) << endl;
|
||||
cout << "U was set to int\n";
|
||||
cout << guy.blab(10.0, 2.3) << endl;
|
||||
cout << "U was set to double\n";
|
||||
cout << "Done\n";
|
||||
return 0;
|
||||
}
|
||||
|
42
base/stl/tempparm.cpp
Normal file
42
base/stl/tempparm.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/9/1.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "stacktp.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
template<template<typename T> class Thing>
|
||||
class Crab {
|
||||
private:
|
||||
Thing<int> s1;
|
||||
Thing<double> s2;
|
||||
public:
|
||||
Crab() {}
|
||||
|
||||
bool push(int a, double x) {
|
||||
return s1.push(a) && s2.push(x);
|
||||
}
|
||||
|
||||
bool pop(int &a, double &x) {
|
||||
return s1.pop(a) && s2.pop(x);
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
Crab<Stack> nebula;
|
||||
int ni;
|
||||
double nb;
|
||||
cout << "Enter int double pairs,such as 4 3.5 (0 0 to end):\n";
|
||||
while (cin >> ni >> nb && ni > 0 && nb > 0) {
|
||||
if (!nebula.push(ni, nb))break;
|
||||
}
|
||||
while (nebula.pop(ni, nb)) {
|
||||
cout << ni << ", " << nb << endl;
|
||||
}
|
||||
cout << "Done.\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
58
base/stl/tmp2tmp.cpp
Normal file
58
base/stl/tmp2tmp.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/9/1.
|
||||
//
|
||||
/**
|
||||
*
|
||||
* 模板类的约束模板友元函数
|
||||
* 非模板友元函数
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template<typename T>
|
||||
void counts();
|
||||
|
||||
template<typename T>
|
||||
void report(T &);
|
||||
|
||||
template<typename TT>
|
||||
class HasFriend {
|
||||
private:
|
||||
TT item;
|
||||
static int ct;
|
||||
public:
|
||||
HasFriend(const TT &i) : item(i) { ct++; };
|
||||
~HasFriend(){ct --;}
|
||||
friend void counts<TT>();
|
||||
friend void report<>(HasFriend<TT> &);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
int HasFriend<T>::ct = 0;
|
||||
|
||||
template<typename T>
|
||||
void counts(){
|
||||
cout <<"template size: "<< sizeof(HasFriend<T>) << "; ";
|
||||
cout <<"template counts(): " <<HasFriend<T>::ct << endl;
|
||||
}
|
||||
template<typename T>
|
||||
void report(T & hf){
|
||||
cout <<"HasFriend<int> : "<<hf.item << endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
counts<int>();
|
||||
HasFriend<int> hfi1(10);
|
||||
HasFriend<int> hfi2(20);
|
||||
HasFriend<double> hfdb(10.5);
|
||||
report(hfi1);
|
||||
report(hfi2);
|
||||
report(hfdb);
|
||||
cout <<"counts<int>() output:\n";
|
||||
counts<int>();
|
||||
cout<<"counts<double>() output:\n";
|
||||
counts<double>();
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user