C++day7(异常处理机制、Lambda表达式、类型转换、STL标准库模板、迭代器、list)

cpp 复制代码
#include <iostream>

using namespace std;
template <typename T>
class vector
{
private:
    T* first;
    T* last;
    T* end;
public:
    vector():first(new T),last(first),end(first){cout<<"无参构造"<<endl;}//无参构造
    vector(T* f):first(f),last(f),end(f)//有参构造
    {cout<<"有参构造"<<endl;}
    vector(const vector &other)://拷贝构造
        first(new T[other.end-other.first]),
        last(first+(other.last-other.first)),
        end(first+(other.end-other.first))
    {
        memcpy(this->first,other.first,sizeof(T)*(other.end-other.first));
    }
    vector & operator=(const vector &other)//拷贝赋值都用深拷贝
    {
        if(this!=&other)
        {
            first = new T[other.end-other.first];
            last = first+(other.last-other.first);
            end = first+(other.end-other.first);
            memcpy(this->first,other.first,sizeof(T)*(other.end-other.first));
        }
        return *this;
    }
    T &at(int index)//返回索引的值
    {
        return *(this->first+index);
    }
    bool empty()//判空
    {
        return first==last?true:false;
    }
    T &front()//返回首部元素
    {
        return *this->first;
    }
    T &back()//返回尾部元素
    {
        return *(this->last-1);
    }
    int size()//求元素个数
    {
        return last-first;
    }
    void clear()//清空
    {
        this->last=this->first;
    }
    void expand()//二倍扩容
    {
        T *temp = new T[2*(this->size()*sizeof(T))];

        memcpy(temp,first,sizeof(T)*this->size());
        delete this->first;
        this->first = nullptr;
        this->last = nullptr;
        this->end = nullptr;
        first = temp;
        last = first+sizeof (T)*this->size();
        end = last+sizeof(T)*this->size();
    }
    void push_back(T value)
    {
        *(this->last++) = value;
        if(this->last==this->end)
            this->expand();
    }
    void pop_back()
    {
        this->last--;
    }

};

int main()
{
    vector<int> V;
    V.push_back(1);
    V.push_back(2);
    V.push_back(3);
    V.push_back(4);
    V.push_back(5);
    cout<<V.at(1)<<endl;
    cout<<V.size()<<endl;
    if(V.empty())
    {
        cout<<"空"<<endl;
    }
    else
        cout<<"非空"<<endl;
    cout<<V.back()<<endl;
    cout<<V.front()<<endl;
    V.pop_back();
    V.pop_back();
    for(int i=0;i<V.size();i++)
    {
        cout<<V.at(i)<<" ";
    }
    cout<<endl;
    V.clear();

    return 0;
}
相关推荐
Molesidy1 天前
【VSCode】【Clangd】Win下的基于LLVM/Clangd+Clangd插件+MINGW+CMake的VSCode配置C/C++开发环境的详细教程
c++·ide·vscode·clangd·llvm
Mr_WangAndy1 天前
C++_chapter13_C++并发与多线程_多线程概念,死锁,unique_lock(),lock_guard()使用
c++·lock·死锁·并发与多线程·unlock·lock_guard·unique_lock
小欣加油1 天前
leetcode 946 验证栈序列
c++·算法·leetcode·职场和发展
神仙别闹1 天前
基于QT(C++) 实现哈夫曼压缩(多线程)
java·c++·qt
无敌最俊朗@1 天前
C++ 并发与同步速查笔记(整理版)
开发语言·c++·算法
神仙别闹1 天前
基于 C++和 Python 实现计算机视觉
c++·python·计算机视觉
眠りたいです1 天前
基于脚手架微服务的视频点播系统-客户端业务逻辑处理部分(三)-客户端主体部分完结
c++·微服务·云原生·架构·json·restful·qt6.7
Elnaij1 天前
从C++开始的编程生活(12)——vector简单介绍和迭代器
开发语言·c++
GISer_Jing1 天前
OSG底层从Texture读取Image实现:readImageFromCurrentTexture
前端·c++·3d
!chen1 天前
CPP 学习笔记 语法总结
c++·笔记·学习