添加stl泛型

This commit is contained in:
zhuyijun 2021-09-01 00:22:05 +08:00
parent 7ea01419f1
commit 2ac1af184d
3 changed files with 21 additions and 8 deletions

View File

@ -58,4 +58,5 @@ add_executable(test5_1 test5.cpp)
add_executable(studentc reusing/studentc.cpp)
add_executable(uses_stuc reusing/uses_stuc.cpp)
add_executable(stacktp stl/stacktp.cpp)
add_executable(stacktp1 stl/stacktp1.cpp)
add_executable(stacktp1 stl/stacktp1.cpp)
add_executable(arraytp stl/arraytp.cpp)

View File

@ -16,10 +16,22 @@ int main() {
int i,j;
for (i = 0; i < 10; ++i) {
sums[i] = 0;
for (int k = 0; k < ; ++k) {
for (j = 0; j < 5; ++j) {
twodee[i][j] = (i +1) * (j +1 );
sums[i] += twodee[i][j];
}
aves[i] = (double )sums[i]/10;
}
for (i = 0; i < 10; ++i) {
for (j = 0; j< 5; ++j) {
cout.width(2);
cout<<twodee[i][j];
}
cout<<" : sum = ";
cout.width(3);
cout <<sums[i] <<", average = " << aves[i]<<endl;
}
cout <<"Done.\n";
return 0;
}

View File

@ -19,11 +19,11 @@ public:
virtual T &operator[](int i);
virtual T operator[](int i) const;
virtual T operator[] (int i) const;
};
template<class T, int n>
ArrayTP<T,n>::ArrayTP<class T, int n>() {
ArrayTP<T,n>::ArrayTP(const T & v) {
for (int i = 0; i < n; ++i) {
ar[i] = v;
}
@ -34,15 +34,15 @@ T & ArrayTP<T,n>::operator[](int i) {
std::cerr <<"Error in array limits: "<< i <<" is out of range\n";
std::exit(EXIT_FAILURE);
}
return a[i];
return ar[i];
}
template<class T, int n>
T & ArrayTP<T,n>::operator[](int i) const {
T ArrayTP<T,n>::operator[](int i) const {
if (i <0 || i >= n){
std::cerr <<"Error in array limits: "<< i <<" is out of range\n";
std::exit(EXIT_FAILURE);
}
return a[i];
return ar[i];
}
#endif //BASE_ARRAYTP_H