自己动手实现vector

有了实现string的基础,在加上一点点模板的知识,就可以自己动手实现一个vector了。下面是我实现的代码,比较简单。有点犯懒了,讲解以后再写吧!

#ifndef MY_VECTOR_H
#define MY_VECTOE_H
#include<cassert>
typedef unsigned int  size_t;
template < class T>
class Vector {
public:
    typedef T * iterator;
    
    Vector();
    Vector(int size, T const& a);
    Vector(const Vector<T> & a);
    ~Vector();
    size_t size() const;
    size_t capacity() const;
    size_t max_capacity() const;
    void push_back(const T& val);
    void pop_back();
    T& operator[](int index);
    Vector<T>& operator=(const Vector<T>& a);
    bool empty() const;
    iterator begin() const;
    iterator end() const;
private:
    size_t  _size;
    size_t  _capacity;
    T*      _buf;
    const size_t _max_capacity = 65536;
};
template<class T>
Vector<T>::Vector()
{   
    _size = 0;
    _buf = new T[1];
    _capacity = 1;
}

template<class T>
Vector<T>::Vector(int s, const T& a)
{   
    if (s > _max_capacity) {
        s = _max_capacity;
    }
    _size = s;
    _capacity = 1;
    while (_capacity < _size) {
        _capacity *= 2;
    }
    _buf = new T[_capacity];
    for (size_t i = 0; i < _size; i++) {
        _buf[i] = a;
    }
}
template<class T>
Vector<T>::Vector(const Vector<T> & a)
{
    _size = a._size;
    _capacity = a._capacity;
    _buf = new T[_capacity];
    for (size_t i = 0; i < _size; i++) {
        _buf[i] = a._buf[i];
    }
}
template<class T>
Vector<T>::~Vector()
{
    delete[] _buf;
}

template<class T>
size_t Vector<T>::size() const
{
    return _size;
}

template<class T>
size_t Vector<T>::capacity() const
{
    return _capacity;
}
template<class T>
size_t Vector<T>::max_capacity() const
{
    return _max_capacity;
}
template<class T>
T& Vector<T>::operator[](int index)
{
    assert(index >= 0 && index < _size);
    return _buf[index];
}

template<class T>
void Vector<T>::push_back(const T& val)
{
    if (_size < _capacity) {
        _buf[_size] = val;
        _size++;
        return ;
    } else if (_size == _max_capacity) {
        return ;
    }
    _capacity *= 2;
    if (_capacity >= _max_capacity) {
        _capacity = _max_capacity;
    }
    T * tmp = new T[_capacity];
    for (size_t i = 0; i < _size; i++) {
        tmp[i] = _buf[i];
    }
    tmp[_size] = val;
    _size++; 
    delete[] _buf;
    _buf = tmp; 
}

template<class T>
void Vector<T>::pop_back()
{
    assert(_size > 0);
    _size--;
}
template<class T>
bool Vector<T>::empty() const
{
    if (_size == 0) {
        return true;
    }
    return false;
}
 // 迭代器的实现
template<class T>
typename Vector<T>::iterator Vector<T>::begin() const
{
    return _buf;
}
template<class T>
typename Vector<T>::iterator Vector<T>::end() const
{
    return _buf + _size;
}
template<class T>
Vector<T>& Vector<T>::operator=(const Vector<T> & a)
{
    if (this == &a) {
        return *this ;
    }
    delete[] _buf;
    _size = a._size;
    _capacity = a._capacity;
    _buf = new T[_capacity];
    for (size_t i = 0; i < _size; i++) {
        _buf[i] = a._buf[i];
    }
    return *this;
}
#endif

测试代码:

#include <iostream>
#include "myvector.h"
#include<string>
using namespace std;
int main()
{
    int c = 20;
    Vector<string> a;
    Vector<string> b;
    for (int i = 0 ; i < 3; i++) {
        a.push_back("hello");
    }
    b = a;
    for (Vector<string>::iterator it = b.begin(); it != b.end(); it++) {
        cout << *it << " " << endl;
    }
    return 0;
}

运行结果:

$ ./a.out
hello
hello
hello
全部评论

相关推荐

烤点老白薯:亲娘嘞🐶💩啊你的简历
点赞 评论 收藏
分享
02-07 12:06
已编辑
华侨大学 测试开发
最近看到很多&nbsp;92&nbsp;的,甚至是硕士,开始往测开赛道卷,说实话有点看不懂。先把话说清楚,大厂里的测开,绝大多数时间干的还是测试的活,只是写点自动化脚本、维护测试平台、接接流水线,真正像开发一样做系统、做架构、做核心平台的测开少得可怜,基本都集中在核心提效组,而且人很少,外面进去的大概率轮不到你,我想真正干过人都清楚。很多人被洗脑了,以为测开也是开,和后端差不多,只是更简单、更轻松、还高薪。现实情况是,测开和开发的职业路径完全不一样。开发的核心是业务和系统能力,测开的核心是稳定性和覆盖率,前者是往上走,后者天花板非常明显。你可以见到很多开发转测开,但你很少见到干了几年测开还能顺利转回开发的。更现实一点说,92&nbsp;的高学历如果拿来做测开,大部分时间就是在做重复性很强的杂活,这种工作对个人能力的放大效应非常弱。三年下来,你和一个双非的,甚至本科的测开差距不会太大,但你和同龄的后端、平台开发差距会非常明显。这不是努不努力的问题,是赛道问题。所谓测开简单高薪,本质上是把极少数核心测开的上限,当成了整个岗位的常态来宣传。那些工资高、技术强的测开,本身就是开发水平,只是挂了个测开的名。普通人进去,99%&nbsp;做的都是项目兜底型工作,而不是你想象中的平台开发。测开不是不能做,但它绝对不是开发的平替,也不是性价比最优解。如果你是真的不想做开发,追求稳定,那测开没问题。但如果你只是觉得测开比后端容易,还能进大厂,那我劝你冷静一点,这只是在用短期安全感换长期天花板。有92的学历,如果你连测开这些重复性工作都能心甘情愿接受,那你把时间精力用在真正的开发、系统、业务深度上,回报大概率比卷测开要高得多。想清楚再下场,别被岗位名和话术带偏了,就算去个前端客户端也是随便占坑的,测开是一个坑位很少赛道,反而大面积学历下放,不用想也能知道会是什么结果,我想各位在JAVA那里已经看到了
小浪_Coding:工作只是谋生的手段 而不是相互比较和歧视
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务