vector里的max_size(),capacity,size(),resize(),reserve()

我的stlport是5.2.1,下面的一些是我看书和看源码得出的,可能有不对的地方,望大家指出

一般知道:

size()是当前元素个数

capacity()是当前可容纳个数

那max_size()又是什么?

看stlport源码如下

 size_type max_size() const {
    size_type __vector_max_size = size_type(-1) / sizeof(_Tp);
    typename allocator_type::size_type __alloc_max_size = this->_M_end_of_storage.max_size();
    return (__alloc_max_seize < __vector_max_size)?__alloc_max_size:__vector_max_size;

可知是对于vector来说是最多可以占用的元素个数。

而resize()和reserve()

resize()重载了一次

resize(size_type,const T&)

resize(size_type)

而resize的源码参见stl源码剖析

void resize(size_type new_size,const T&x)
{
  if(new_size<size())
      erase(begin()+new_size,end());
  else
      insert(end(),new_size-size(),x);
}

void resize(size_type new_size)
{
   resize(new_size,T());
}

可以发现几点

 

1.resize可以“缩小“size,而且是从前面开始erase掉,而erase是不会造成reallocate的,所以如果new_size<size(),capacity不变。

2.resize可以扩大capacity,如果new_size大于等于size(),引起insert,insert是可能造成reallocate的,那么也就是说,既然reallocate了,迭代器也可能失效。

3.当然对与vector内的型别,如果自定义,必须得定义默认构造函数.

 

再看reserve ,stl源码剖析上似乎没看到reserve,进入调试看了下

源码如下

template <class _Tp, class _Alloc>
void vector<_Tp, _Alloc>::reserve(size_type __n) {
  if (capacity() < __n) {
    if (max_size() < __n) {
      this->_M_throw_length_error();
    }

    const size_type __old_size = size();
    pointer __tmp;
    if (this->_M_start) {
      __tmp = _M_allocate_and_copy(__n, this->_M_start, this->_M_finish);
      _M_clear();
    } else {
      __tmp = this->_M_end_of_storage.allocate(__n, __n);
    }
    _M_set(__tmp, __tmp + __old_size, __tmp + __n);
  }
}

可以发现:

1.若capacity()>=_n时,reserve是直接返回的.不做任何操作

2.这里我debug了一下,发现stlport5.2.1和书里讲的还是有点不一样,首先end_of_storage不再是pointer类型,而是被一个alloc_proxy封装起来,不过总的来说还是得重新分配内存,而且reserve后的capacity就是参数__n.

同时把原有元素拷贝过去.这里额外提一下,vector<t>()默认构造capacity==0,是没有分配空间的.而vector<t>(size_type n,const T&) size==capacity,同时迭代器失效也是当然.

 

转载于:https://www.cnblogs.com/cavehubiao/p/3656785.html

写一个在vs2019上能运行的#include <iostream>#include <stdlib.h>using namespace std;template <typename T>class Vector{public: Vector() : m_size(0), m_capacity(0), m_data(nullptr) {} Vector(int n, const T& val) : m_size(0), m_capacity(0), m_data(nullptr) { assign(n, val); } Vector(const Vector& other) : m_size(0), m_capacity(0), m_data(nullptr) { assign(other); } Vector& operator=(const Vector& other); T& operator[](int i) { return m_data[i]; } const T& operator[](int i) const { return m_data[i]; } void push_back(const T& val); void insert(int pos, const T& val); void clear(); int size() const { return m_size; } bool empty() const { return m_size == 0; } void erase(int pos);private: void assign(int n, const T& val); void assign(const Vector& other); void reserve(int n); void resize(int n); void destroy();private: int m_size; int m_capacity; T* m_data;};template <typename T>Vector<T>& Vector<T>::operator=(const Vector<T>& other){ if (this != &other) { destroy(); assign(other); } return *this;}template <typename T>void Vector<T>::push_back(const T& val){ if (m_size == m_capacity) { reserve(max(2 * m_capacity, 1)); } m_data[m_size++] = val;}template <typename T>void Vector<T>::insert(int pos, const T& val){ if (pos < 0 || pos > m_size) { return; } if (m_size == m_capacity) { reserve(max(2 * m_capacity, 1)); } for (int i = m_size - 1; i >= pos; i--) { m_data[i + 1] = m_data[i]; } m_data[pos] = val; m_size++;}template <typename T>void Vector<T>::clear(){ destroy(); m_size = 0;}template <typename T>void Vector<T>::erase(int pos){ if (pos < 0 || pos >= m_size) { return; } for (int i = pos; i < m_size - 1; i++) { m_data[i] = m_data[i + 1]; } m_size--;}template <typename T>void Vector<T>::assign(int n, const T& val){ resize(n); for (int i = 0; i < m_size; i++) { m_data[i] = val; }}template <typename T>void Vector<T>::assign(const Vector<T>& other){ resize(other.m_size); for (int i = 0; i < m_size; i++) { m_data[i] = other.m_data[i]; }}template <typename T>void Vector<T>::reserve(int n){ if (n <= m_capacity) { return; } T* new_data = new T[n]; for (int i = 0; i < m_size; i++) { new_data[i] = m_data[i]; } delete[] m_data; m_data = new_data; m_capacity = n;}template <typename T>void Vector<T>::resize(int n){ reserve(n); if (n >= m_size) { for (int i = m_size; i < n; i++) { m_data[i] = T(); } } m_size = n;}template <typename T>void Vector<T>::destroy(){ if (m_data != nullptr) { delete[] m_data; m_data = nullptr; m_capacity = 0; }}int main(){ Vector<int> vec; cout << "push_back 1, 2, 3" << endl; vec.push_back(1); vec.push_back(2); vec.push_back(3); cout << "size: " << vec.size() << endl; cout << "empty: " << vec.empty() << endl; cout << "insert 0 at pos 0" << endl; vec.insert(0, 0); cout << "size: " << vec.size() << endl; cout << "erase at pos 1" << endl; vec.erase(1); cout << "size: " << vec.size() << endl; cout << "clear" << endl; vec.clear(); cout << "size: " << vec.size() << endl; cout << "empty: " << vec.empty() << endl; return 0;}
04-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值