push_back 和 pop_back 的实施
Implementation of push_back and pop_back
我想了解 std::vector<T>::push_back
和 std::vector<T>::pop_back
如何在分配的内存中创建和销毁对象?
我用了 google,我发现人们只是玩 size
和 capacity
来限制对内部动态数组的访问,但我不认为事情是这样的在标准实现中确实有效
注意:我不要求标准实现,因为它会很复杂,但我希望能有这种方法的基本实现
编辑: 我想出了如何实现我自己的自定义分配器
为简单起见,我将仅显示自定义分配器中的重要函数
template <typename T>
T* allocate(std::size_t count) {
return static_cast<T*>(::operator new(count * sizeof(T)));
}
template <typename T>
void deallocate(T* ptr, std::size_t count) {
operator delete(ptr);
}
template <typename U, typename... Args>
void construct(U* ptr, Args&&... args) {
new(ptr) U(std::forward<Args>(args)...);
}
template <typename U>
void destroy(U* ptr) {
ptr->~U();
}
然后我在我自己定义的向量中使用像这样的东西
int* buff = allocate<int>(8);
// This is like:
// std::vector<int> vec;
// vec.reserve(8);
int* last = &buff[0];
construct<int>(last, 32);
// This is like:
// vec.push_back(32);
++last;
construct<int>(last, 12);
// This is another push
// vec.push_back(12);
destroy(last);
--last;
// This is like:
// vec.pop_back();
deallocate(buff, 8);
// This shoud be in:
// ~vector();
如有遗漏请查收...谢谢
所有带有分配器的标准容器都使用分配器来构造或销毁元素:
23.2.1 [3] 一般容器要求 (N4296)
For the components affected by this subclause that declare an
allocator_type, objects stored in these components shall be
constructed using the allocator_traits::construct
function and destroyed using the
allocator_traits::destroy function
标准库中的默认分配器使用 placement new 来构造并调用析构函数来销毁元素:
20.7.9 [11] 和 [12] 默认分配器 (N4296)
template <class U, class... Args>
void construct(U* p, Args&&... args);
效果:::new((void *)p) U(std::forward(args)...)
template <class U>
void destroy(U* p);
效果:p->~U()
我想了解 std::vector<T>::push_back
和 std::vector<T>::pop_back
如何在分配的内存中创建和销毁对象?
我用了 google,我发现人们只是玩 size
和 capacity
来限制对内部动态数组的访问,但我不认为事情是这样的在标准实现中确实有效
注意:我不要求标准实现,因为它会很复杂,但我希望能有这种方法的基本实现
编辑: 我想出了如何实现我自己的自定义分配器
为简单起见,我将仅显示自定义分配器中的重要函数
template <typename T>
T* allocate(std::size_t count) {
return static_cast<T*>(::operator new(count * sizeof(T)));
}
template <typename T>
void deallocate(T* ptr, std::size_t count) {
operator delete(ptr);
}
template <typename U, typename... Args>
void construct(U* ptr, Args&&... args) {
new(ptr) U(std::forward<Args>(args)...);
}
template <typename U>
void destroy(U* ptr) {
ptr->~U();
}
然后我在我自己定义的向量中使用像这样的东西
int* buff = allocate<int>(8);
// This is like:
// std::vector<int> vec;
// vec.reserve(8);
int* last = &buff[0];
construct<int>(last, 32);
// This is like:
// vec.push_back(32);
++last;
construct<int>(last, 12);
// This is another push
// vec.push_back(12);
destroy(last);
--last;
// This is like:
// vec.pop_back();
deallocate(buff, 8);
// This shoud be in:
// ~vector();
如有遗漏请查收...谢谢
所有带有分配器的标准容器都使用分配器来构造或销毁元素:
23.2.1 [3] 一般容器要求 (N4296)
For the components affected by this subclause that declare an allocator_type, objects stored in these components shall be constructed using the allocator_traits::construct function and destroyed using the allocator_traits::destroy function
标准库中的默认分配器使用 placement new 来构造并调用析构函数来销毁元素:
20.7.9 [11] 和 [12] 默认分配器 (N4296)
template <class U, class... Args>
void construct(U* p, Args&&... args);
效果:::new((void *)p) U(std::forward(args)...)
template <class U>
void destroy(U* p);
效果:p->~U()