看了紫书上讲二叉树的时候说的那个内存池,感觉挺有意思,想自己实现下,于是写了这么一个人不像人鬼不像鬼像代码的代码,也不知道是不是内存池
有大佬能判定下这算内存池吗qwq
#include <iostream>
#include <queue>
template <class T>
class memory_pool {
private:
std::queue<T*> freePool;
public:
memory_pool(int size = 0);
~memory_pool();
T* takeout();
// void putinto();
};
template <class T>
memory_pool<T>::memory_pool(int size) {
for (int i = 0; i < size; i++) {
T* ptr = new T();
freePool.push(ptr);
}
std::cout << "Memory Pool Initialized.\n";
}
template <class T>
memory_pool<T>::~memory_pool() {
while (!freePool.empty()) {
delete freePool.front();
freePool.pop();
}
std::cout << "Memory Pool Empty\n";
}
template <class T>
T* memory_pool<T>::takeout() {
T* ptr = freePool.front();
freePool.pop();
return ptr;
}
struct node {
int a;
int b;
node(int arg_a = 10, int arg_b = 9) : a(arg_a), b(arg_b){};
};
int main() {
node* arr[1001];
int n;
in:
std::cout << "Input a num(Memory Pool's size): ";
std::cin >> n;
if (n <= 999)
std::cout << "done.\n";
else {
std::cout << "Size is out of the range.\n";
std::cout << "\nreinput:\n";
goto in;
}
memory_pool<node> p(n);
for (int i = 0; i < n; i++) {
int tmp, tmp2;
std::cin >> tmp >> tmp2;
arr[i] = p.takeout();
arr[i]->a = tmp;
arr[i]->b = tmp2;
}
for (int i = 0; i < n; i++) {
std::cout << arr[i]->a << " " << arr[i]->b << std::endl;
}
system("pause");
return 0;
}
还有就是,这种东西应该发到学术版还是灌水区啊,,不敢发到学术版