当前位置: 首页 > news >正文

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

 

#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;}elsecout<<"非空"<<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;
}

http://www.lryc.cn/news/96738.html

相关文章:

  • 性能优化问题
  • 【云原生系列】云计算概念与架构设计介绍
  • Swoole协程系统HTTP服务
  • SpringCloud学习路线(13)——分布式搜索ElasticSeach集群
  • CMIP6数据处理及在气候变化、水文、生态等领域中的应用
  • hadoop之mapreduce详解
  • leetcode做题笔记44
  • mac brew安装 node 踩坑日记- n切换node不生效
  • 数据预处理matlab
  • ubuntu18.04安装autoware1.15
  • 在CSDN学Golang云原生(Docker基础)
  • Zookeeper命令总结
  • C语言中的函数(超详细)
  • 华为H3C思科网络设备命令对照表
  • 产品需求、系统架构设计经验篇
  • 关于websocket的几点注意事项
  • go学习 4、复合数据类型
  • Rust: Vec类型的into_boxed_slice()方法
  • Python - Opencv + pyzbar实时摄像头识别二维码
  • 网络安全(黑客)就业分析指导
  • MySQL 主从复制的认识 2023.07.23
  • elasticsearch查询操作(API方式)
  • Java版企业工程项目管理系统源码+java版本+项目模块功能清单+spring cloud +spring boot
  • 理解Android中不同的Context
  • linux判断端口是否占用(好用)
  • springboot 自定义注解 ,实现接口限流(计数器限流)【强行喂饭版】
  • istio安装部署总结
  • Linux操作系统~必考面试题⑨
  • 国标GB28181协议视频平台EasyCVR修改录像计划等待时间较长的原因排查与解决
  • 线性代数(主题篇):第三章:向量组 、第四章:方程组