std::vector源码
时间: 2023-07-20 16:38:10 浏览: 246
`std::vector`是C++标准库中的一个容器,用于存储动态大小的数组。下面是`std::vector`的简化源码:
```c++
template<typename T, typename Allocator = std::allocator<T>>
class vector {
public:
using value_type = T;
using allocator_type = Allocator;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = typename std::allocator_traits<Allocator>::pointer;
using const_pointer = typename std::allocator_traits<Allocator>::const_pointer;
using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
// 构造函数
vector() noexcept;
explicit vector(const Allocator& alloc) noexcept;
explicit vector(size_type count, const T& value, const Allocator& alloc = Allocator());
explicit vector(size_type count, const Allocator& alloc = Allocator());
template<typename InputIt>
vector(InputIt first, InputIt last, const Allocator& alloc = Allocator());
vector(const vector& other);
vector(const vector& other, const Allocator& alloc);
vector(vector&& other) noexcept;
vector(vector&& other, const Allocator& alloc) noexcept;
vector(std::initializer_list<T> init, const Allocator& alloc = Allocator());
// 析构函数
~vector();
// 大小相关操作
size_type size() const noexcept;
size_type max_size() const noexcept;
void resize(size_type count);
void resize(size_type count, const value_type& value);
size_type capacity() const noexcept;
bool empty() const noexcept;
void reserve(size_type new_cap);
// 访问元素
reference operator[](size_type pos);
const_reference operator[](size_type pos) const;
reference at(size_type pos);
const_reference at(size_type pos) const;
reference front();
const_reference front() const;
reference back();
const_reference back() const;
T* data() noexcept;
// 迭代器操作
iterator begin() noexcept;
const_iterator begin() const noexcept;
const_iterator cbegin() const noexcept;
iterator end() noexcept;
const_iterator end() const noexcept;
const_iterator cend() const noexcept;
reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;
const_reverse_iterator crbegin() const noexcept;
reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;
const_reverse_iterator crend() const noexcept;
// 修改容器
void clear() noexcept;
iterator insert(const_iterator pos, const T& value);
iterator insert(const_iterator pos, T&& value);
iterator insert(const_iterator pos, size_type count, const T& value);
template<typename InputIt>
iterator insert(const_iterator pos, InputIt first, InputIt last);
iterator insert(const_iterator pos, std::initializer_list<T> init);
iterator erase(const_iterator pos);
iterator erase(const_iterator first, const_iterator last);
void push_back(const T& value);
void push_back(T&& value);
template<typename... Args>
reference emplace_back(Args&&... args);
void pop_back();
void swap(vector& other) noexcept;
// 分配器相关操作
allocator_type get_allocator() const noexcept;
};
```
`std::vector`的源码实现非常复杂,其中包含大量的模板和细节处理。但是,可以看到,它提供了许多方便的方法来访问和修改元素,以及管理容器的大小和内存分配。同时,它还支持迭代器,使得可以在容器中进行遍历和修改。
阅读全文