【发布时间】:2020-07-02 06:15:24
【问题描述】:
我正在尝试学习和编写一个自定义分配器 - 我期待应该打印 cout 语句,但它从来没有 - 我做错了什么 - 如何编写自定义分配器:
#include <iostream>
#include <vector>
template < class T >
class MyAllocator : public std::allocator<T> {
public:
T* allocate(size_t size)
{
std::cout << "Allocation request size " << size << std::endl;
return new T[size];
}
};
int main()
{
std::vector <int, MyAllocator<int>> x;
x.push_back(10);
x.push_back(10);
x.push_back(10);
for (auto& var : x)
std::cout << "Value " << var << std::endl;
}
输出
Value 10
Value 10
Value 10
【问题讨论】:
标签: c++ c++11 visual-c++ c++14