
- #include
-
- using namespace std;
-
- template <typename V>
- class Myvector{
- private:
- V *data;
- int Capacity;
- int Size;
- public:
- //无参构造
- Myvector():data(new V[Capacity]),Capacity(0),Size(0){
- cout<<"无参构造函数"<
- }
- //有参构造
- Myvector(int c,int s):Capacity(c),Size(s){
- data=new V[Capacity];
- cout<<"有参构造函数"<
- }
- //析构函数
- ~Myvector(){
- delete []data;
- cout<<"析构函数"<
- }
- //拷贝构造函数
- Myvector(const Myvector &other):data(new V[other.Capacity]),Capacity(other.Capacity),Size(other.Size){
- for (int i = 0; i < Size; i++) {
- data[i] = other.data[i];
- }
- cout<<"拷贝构造函数"<
- }
- //拷贝赋值函数
- Myvector &operator= (const Myvector &other){
- if(this!=&other){
- this->Capacity=other.Capacity;
- this->Size=other.Size;
- }
- if(this->data!=NULL){
- delete this->data;
- }
- this->data=new V(*other.data);
- for (int i = 0; i < Size; i++) {
- data[i] = other.data[i];
- }
- cout<<"拷贝赋值函数"<
- return *this;
- }
- //判空函数
- bool empty(){
- return 0==Size;
- }
-
- //capacity函数
- int capacity(){
- return Capacity;
- }
-
- //size函数
- int size(){
- return Size;
- }
- //插入函数
- void insert(int pos,V e){
-
- if(pos<0||pos>Size){
- throw string("下标不能小于0,不能超过Size");
- }
- else{
- if(Size==Capacity){
- V* temp=new V[Capacity*2];
- for (int i = 0; i < Size; i++) {
- temp[i] = data[i];
- }
- delete this->data;
- this->data=temp;
- this->Capacity*=2;
- }
- for(int i=Size;i>pos;i--){
- data[i]=data[i-1];
- }
- data[pos]=e;
- Size++;
- }
- cout<<"插入成功"<
-
- }
-
- //show函数
- void show(){
- for(int i=0;i
- cout<'\t';
- }
- cout<
- }
- };
-
- int main()
- {
- Myvector<int> v(5,0);
- cout<<"容量:"<
capacity()< - cout<<"元素数量:"<
size()< - v.insert(0,1);
- v.insert(0,2);
- v.insert(0,3);
- v.insert(0,4);
- v.insert(0,5);
- v.show();
- v.insert(0,6);
- cout<<"容量:"<
capacity()< - cout<<"元素数量:"<
size()< - v.insert(0,7);
- v.insert(0,8);
- v.insert(0,9);
- v.insert(0,10);
- v.insert(0,11);
- v.insert(0,12);
- cout<<"容量:"<
capacity()< - cout<<"元素数量:"<
size()< - v.show();
- return 0;
- }
-
相关阅读:
SpringMVC概述及入门
【分享】“飞书第三方“在集简云平台集成应用的常见问题与解决方案
HTTPS 的加密流程
Spring MVC 工作流程源码分析
nginx--技术文档--架构体系--底层核心-原理
Kafka消费者
Spring的创建和使用
Android 老开发被薪资竟然被应届生倒挂了……
Postgresql顺滑升级步骤(11升级到14)
云服务器上部署仿牛客网项目
-
原文地址:https://blog.csdn.net/qq_53268516/article/details/132890984
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU