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;
}
相关推荐
范纹杉想快点毕业28 分钟前
解析Qt文件保存功能实现
java·开发语言·c++·算法·命令模式
C++ 老炮儿的技术栈33 分钟前
C++实现手写strlen函数
大数据·c语言·c++·编辑器
2301_799084672 小时前
Codeforces Round 1032 (Div. 3)
数据结构·c++·算法
岁忧2 小时前
(LeetCode 每日一题) 2016. 增量元素之间的最大差值 (数组)
java·c++·算法·leetcode·职场和发展·go
AI+程序员在路上3 小时前
ABI与API定义及区别
c语言·开发语言·c++
charlie1145141913 小时前
从C++编程入手设计模式——装饰器模式
c++·设计模式·装饰器模式
不太聪明的样子5 小时前
c++ 项目使用 prometheus + grafana 进行实时监控
c++·grafana·prometheus
滴滴滴嘟嘟嘟.5 小时前
FreeRTOS 任务管理学习笔记
c++·嵌入式硬件·freertos
咩咩大主教5 小时前
2025最新版使用VSCode和CMake图形化编译调试Cuda C++程序(保姆级教学)
c++·vscode·cmake·visual studio·cuda·cpp·cuda c++
虾球xz6 小时前
CppCon 2017 学习:folly::Function A Non-copyable Alternative to std::function
开发语言·c++·学习