更新:仿函数、函数模板、成员模板、模板特化。
This commit is contained in:
parent
5afd86f6d4
commit
359e82b396
@ -3,4 +3,4 @@ project(MSTL)
|
|||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 14)
|
set(CMAKE_CXX_STANDARD 14)
|
||||||
|
|
||||||
add_executable(MSTL main.cpp h/String.h h/List.h h/Singleton.h h/Faction.h h/shared_ptr.h)
|
add_executable(MSTL main.cpp h/String.h h/List.h h/Singleton.h h/Faction.h h/shared_ptr.h h/function_like_classes.h h/function_template.h h/member_template.h h/specialization.h)
|
||||||
|
Binary file not shown.
@ -1,7 +1,3 @@
|
|||||||
# ninja log v5
|
# ninja log v5
|
||||||
0 197 1664367911765168078 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
|
||||||
197 247 1664367911813167618 MSTL 5fce564c316cd3aa
|
|
||||||
1 201 1664369005344443406 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
|
||||||
201 254 1664369005396441968 MSTL 5fce564c316cd3aa
|
|
||||||
0 210 1664369061766923384 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
0 210 1664369061766923384 CMakeFiles/MSTL.dir/main.cpp.o 134556d569115d52
|
||||||
210 264 1664369061818922019 MSTL 5fce564c316cd3aa
|
210 264 1664369061818922019 MSTL 5fce564c316cd3aa
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
Start testing: Sep 28 20:44 CST
|
Start testing: Sep 29 15:44 CST
|
||||||
----------------------------------------------------------
|
----------------------------------------------------------
|
||||||
End testing: Sep 28 20:44 CST
|
End testing: Sep 29 15:44 CST
|
||||||
|
16
h/function_like_classes.h
Normal file
16
h/function_like_classes.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
//
|
||||||
|
// Created by dongl on 22-9-28.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef MSTL_FUNCTION_LIKE_CLASSES_H
|
||||||
|
#define MSTL_FUNCTION_LIKE_CLASSES_H
|
||||||
|
|
||||||
|
#endif //MSTL_FUNCTION_LIKE_CLASSES_H
|
||||||
|
|
||||||
|
/// 仿函数 类 看起来像一个 函数
|
||||||
|
template<class T>
|
||||||
|
struct identity{
|
||||||
|
const T& operator()(const T& x) const {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
};
|
15
h/function_template.h
Normal file
15
h/function_template.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
//
|
||||||
|
// Created by dongl on 22-9-29.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef MSTL_TEM_FUNCTION_H
|
||||||
|
#define MSTL_TEM_FUNCTION_H
|
||||||
|
|
||||||
|
#endif //MSTL_TEM_FUNCTION_H
|
||||||
|
|
||||||
|
|
||||||
|
/// 函数模板
|
||||||
|
template<typename T>
|
||||||
|
T& min(const T& var1, const T& var2) {
|
||||||
|
return var1 < var2 ? var2 : var1;
|
||||||
|
}
|
22
h/member_template.h
Normal file
22
h/member_template.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
//
|
||||||
|
// Created by dongl on 22-9-29.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef MSTL_MEMBER_TEMPLATE_H
|
||||||
|
#define MSTL_MEMBER_TEMPLATE_H
|
||||||
|
|
||||||
|
#endif //MSTL_MEMBER_TEMPLATE_H
|
||||||
|
|
||||||
|
/// 成员模板
|
||||||
|
template<class T1, class T2>
|
||||||
|
struct pair{
|
||||||
|
T1 first;
|
||||||
|
T2 second;
|
||||||
|
|
||||||
|
pair() : first(T1()), second(T2()){}
|
||||||
|
pair(const T1& a, const T2& b) : first(a), second(b) {}
|
||||||
|
|
||||||
|
// 成员模板
|
||||||
|
template<class U1, class U2>
|
||||||
|
pair(const pair<U1,U2>& p) : first(p.first), second(p.second){}
|
||||||
|
};
|
@ -23,4 +23,6 @@ public:
|
|||||||
private:
|
private:
|
||||||
T* px;
|
T* px;
|
||||||
long* pn;
|
long* pn;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
61
h/specialization.h
Normal file
61
h/specialization.h
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
//
|
||||||
|
// Created by dongl on 22-9-29.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
#ifndef MSTL_SPECIALIZATION_H
|
||||||
|
#define MSTL_SPECIALIZATION_H
|
||||||
|
|
||||||
|
#endif //MSTL_SPECIALIZATION_H
|
||||||
|
|
||||||
|
/// 模板特化 specialization
|
||||||
|
template<class type>
|
||||||
|
struct hash{
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct hash<char>{
|
||||||
|
size_t operator() (char x) const {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct hash<int>{
|
||||||
|
size_t operator() (int x) const {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct hash<long>{
|
||||||
|
size_t operator() (long x) const {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/// partial specialization 模板偏特化 局部特化 个数上的偏
|
||||||
|
template<typename T, typename Alloc>
|
||||||
|
class m_vector{
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename Alloc>
|
||||||
|
class m_vector<bool, Alloc>{
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/// partial specialization 模板偏特化 局部特化 范围上的偏
|
||||||
|
template<typename T>
|
||||||
|
class C{
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class C<T*>{
|
||||||
|
|
||||||
|
};
|
9
main.cpp
9
main.cpp
@ -3,6 +3,8 @@
|
|||||||
#include "h/String.h"
|
#include "h/String.h"
|
||||||
#include "h/Faction.h"
|
#include "h/Faction.h"
|
||||||
#include "h/shared_ptr.h"
|
#include "h/shared_ptr.h"
|
||||||
|
#include "h/function_like_classes.h"
|
||||||
|
#include "h/specialization.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
List<double> list = *new List<double>;
|
List<double> list = *new List<double>;
|
||||||
@ -18,5 +20,12 @@ int main() {
|
|||||||
shared_ptr<String> ptr(new String("ptr"));
|
shared_ptr<String> ptr(new String("ptr"));
|
||||||
std::cout << ptr->get_c_str() << std::endl;
|
std::cout << ptr->get_c_str() << std::endl;
|
||||||
|
|
||||||
|
|
||||||
|
String str("fun_str");
|
||||||
|
|
||||||
|
// identity<int> ity()(1000);
|
||||||
|
|
||||||
|
hash<long>()(1000);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user