扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
template>class vector {// varying size array of values
private:
templatefriend class _Vb_val;
friend _Tidy_guard;
using _Alty = _Rebind_alloc_t<_Alloc, _Ty>;
using _Alty_traits = allocator_traits<_Alty>;
...........
template>class list {// bidirectional linked list
private:
templatefriend class _Hash;
templatefriend bool _Hash_equal(const _Hash<_Traits>&, const _Hash<_Traits>&);
using _Alty = _Rebind_alloc_t<_Alloc, _Ty>;
using _Alty_traits = allocator_traits<_Alty>;
using _Node = _List_node<_Ty, typename allocator_traits<_Alloc>::void_pointer>;
using _Alnode = _Rebind_alloc_t<_Alloc, _Node>;
using _Alnode_traits = allocator_traits<_Alnode>;
可以看到STL中vector 和 list的分配器,是采用编译器默认提供的 allocator 分配器。
#include#include#include#include#include#include#include
#include#includeusing namespace std;
class Xdata
{public:
int m_index;
Xdata()
{cout<< "call Xdata()"<< endl;
}
Xdata(const Xdata& obj)
{this->m_index = obj.m_index;
cout<< "call Xdata(const Xdata& obj)"<< endl;
}
~Xdata()
{cout<< "call ~Xdata()"<< endl;
}
};
templateclass MyAllocator
{public:
using value_type = T;
MyAllocator() {}
templateMyAllocator(const MyAllocator&) {}
T* allocate(const size_t count)
{cout<< "T* allocate(const size_t count)"<< endl;
cout<< "typeid(T).name():"<< typeid(T).name()<< endl;
return static_cast(malloc(sizeof(T)*count));
}
void deallocate(T* const ptr, const size_t count)
{cout<< "void deallocate(T* const ptr, const size_t count)"<< endl;
free(ptr);
}
};
int main() {vector>vec;
Xdata x1;
x1.m_index = 1;
vec.push_back(x1);
cout<< "==========================="<< endl;
list>list_data;
Xdata x2;
x2.m_index = 2;
list_data.push_back(x2);
return 0;
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流