实现自己简单vector, 使用特化类型

复制构造函数还没写,先这样吧,用编译器给的浅复制 

#include <iostream>
#include <cstdio>
#include "memory.h"

struct type_false{
    bool get(){
        return false;
    }
};
struct type_ture{
    bool get(){
        return true;
    }
};
#define CASE(input) \
template <>\
struct input_type<input>{\
    using _type_foo = struct type_ture;\
};
template <class T>
struct input_type{
    using _type_foo = struct type_false;
};
CASE(char);
CASE(double);
CASE(long long);
CASE(float);
CASE(int);
CASE(unsigned int);

template <typename T>
class my_vector{
public:
    my_vector(){
        if(typename input_type<T>::_type_foo().get()){
            _size = 0;
            _capacity = 3;
            head = (T *)malloc(_capacity * sizeof(T));
        } else {
            head = new T[3];
            _size = 0;
            _capacity = 3;
        }
    };
    my_vector(const T& input){
        if(typename input_type<T>::_type_foo().get()){
            head = malloc(input._capacity * sizeof(T));
            memcpy(head, input.head, input._size);
            _size = input._size;
            _capacity = input._capacity;
        }
    };
    void push_back(T const input){
        _chack_size();
        this->head[_size] = input;
        _size++;
    }
    void emplace_back(T &&input){
        _chack_size();
        this->head[_size++] = std::move(input);
    }
    ~my_vector(){
        if(typename input_type<T>::_type_foo().get()){
            free(head);
        }
        else
            delete[] head;
    }
    T& operator[](int const input){
        if(input >= _size) {
            std::cerr << "溢出" << std::endl;
            exit(1);
        }
        return *(head + input);
    }
    size_t size(){
        return _size;
    }
    void _chack_size(){
        if(_size >= _capacity){
            if(typename input_type<T>::_type_foo().get()){
                T * temp = (T*) malloc((_capacity * 2) * sizeof(T));
                memcpy(temp, head, _size * sizeof(T));
                _capacity = _capacity * 2;
                free(head);
                head = temp;
            }
            else{
                T *temp = new T[(_capacity * 2)];
                for(int i = 0; i < _size; i++)
                    temp[i] = head[i];
                _capacity = _capacity * 2;
                delete[] head;
                head = temp;
            }
        }
    }
private:
    T *head;
    size_t _size;
    size_t _capacity;
};

int main(){
    //test1
    my_vector<int> a;
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);
    a.push_back(4);
    a.push_back(5);
    a.push_back(6);
    a.push_back(7);
    a.push_back(8);
    a.push_back(9);

    for(int i = 0; i < a.size(); i++)
        std::cout << a[i] << std::endl;
test2
    my_vector<int> q(a);
    for(int i = 0; i < q.size(); i++)
        std::cout << q[i] << std::endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值