实现 stl 中的 vector 操作。

1、MyVector.h

#pragma once

#include <iostream>
using namespace std; template <typename T>
class MyVector
{
friend ostream& operator<< <T>(ostream &out, MyVector<T> &obj);
public:
MyVector(int size = );//构造函数
MyVector(const MyVector &obj);//拷贝构造函数
~MyVector();//析构函数 T& operator[] (int index);
MyVector &operator=(const MyVector &obj); int getLen() {
return m_len;
}
protected:
T *m_space;
T m_len;
};

2、MyVector.cpp

#include <iostream>
using namespace std;
#include "MyVector.h" template <typename T>
MyVector<T>::MyVector(int size = ) //构造函数
{
m_space = new T[size];
m_len = size;
}
template <typename T>
MyVector<T>::MyVector(const MyVector &obj)//拷贝构造函数
{
m_len = obj.m_len;
m_space = new T[m_len]; for (int i = ; i < m_len; i++)
{
m_space[i] = obj.m_space[i];
}
}
template <typename T>
MyVector<T>::~MyVector()//析构函数
{
if (m_space != nullptr)
{
delete[] m_space;
m_space = nullptr;
m_len = ;
}
}
//ostream& operator<< <T>(ostream &out, MyVector<T> &obj)
template <typename T>
ostream& operator<< (ostream &out, MyVector<T> &obj)
{
for (int i = ; i < obj.m_len; i++)
{
out << obj.m_space[i] << " ";
}
out << endl;
return out;
} template <typename T>
T& MyVector<T>::operator[] (int index)
{
return m_space[index];
} template <typename T>
MyVector<T> & MyVector<T>::operator=(const MyVector<T> &obj)
{
if (m_space != nullptr)
{
delete[] m_space;
m_space = nullptr;
m_len = ;
}
m_len = obj.m_len;
m_space = new T[m_len];
for (int i = ; i < m_len; i++)
{
m_space[i] = obj[i];
} return *this;
}

3、MyVector.cpp(测试函数)

#include <iostream>
using namespace std;
#include "MyVector.cpp" void main()
{
MyVector<int> mv1();
for (int i = ; i < mv1.getLen(); i++)
{
mv1[i] = i + ;
cout << mv1[i] << endl;
}
cout << endl; MyVector<int> mv2 = mv1;
for (int i = ; i < mv2.getLen(); i++)
{
mv2[i] = i + ;
}
cout << mv2 << endl; system("pause"); }

C++(四十四) — 数组模板类(vector工具)的更多相关文章

  1. NeHe OpenGL教程 第四十四课:3D光晕

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  2. 模板类 vector

    概要 介绍一下模板类 vector 的常用操作,以及一个应用举例,顺时针打印矩阵.   基本定义 模板类 vector 是一种动态数组,它是使用 new 创建动态数组的替代品,实际上,vector 也 ...

  3. 网站开发进阶(四十四)input type="submit" 和"button"的区别

    网站开发进阶(四十四)input type="submit" 和"button"的区别   在一个页面上画一个按钮,有四种办法: 这就是一个按钮.如果你不写ja ...

  4. Gradle 1.12用户指南翻译——第四十四章. 分发插件

    本文由CSDN博客貌似掉线翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  5. SQL注入之Sqli-labs系列第四十一关(基于堆叠注入的盲注)和四十二关四十三关四十四关四十五关

    0x1普通测试方式 (1)输入and1=1和and1=2测试,返回错误,证明存在注入 (2)union select联合查询 (3)查询表名 (4)其他 payload: ,( ,( 0x2 堆叠注入 ...

  6. “全栈2019”Java第四十四章:继承

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  7. 孤荷凌寒自学python第四十四天Python操作 数据库之准备工作

     孤荷凌寒自学python第四十四天Python操作数据库之准备工作 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天非常激动地开始接触Python的数据库操作的学习了,数据库是系统化设计 ...

  8. Android项目实战(四十四):Zxing二维码切换横屏扫描

    原文:Android项目实战(四十四):Zxing二维码切换横屏扫描 Demo链接 默认是竖屏扫描,但是当我们在清单文件中配置横屏显示的时候: <activity android:name=&q ...

  9. 第四十四个知识点:在ECC密码学方案中,描述一些基本的防御方法

    第四十四个知识点:在ECC密码学方案中,描述一些基本的防御方法 原文地址:http://bristolcrypto.blogspot.com/2015/08/52-things-number-44-d ...

  10. Spark(四十四):使用Java调用spark-submit.sh(支持 --deploy-mode client和cluster两种方式)并获取applicationId

    之前也介绍过使用yarn api来submit spark任务,通过提交接口返回applicationId的用法,具体参考<Spark2.3(四十):如何使用java通过yarn api调度sp ...

随机推荐

  1. 【linux基础】ubuntu实现双屏显示

    前言 之前博主没有使用NVIDIA时候已经可以实现双屏显示(拼接类型),但是,安装NVIDIA驱动使用CUDA之后这个功能就消失了,需要重新配置. 实现方式 1. 使用Intel集成显卡时实现双屏拼接 ...

  2. [LeetCode] 220. Contains Duplicate III 包含重复元素 III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  3. [LeetCode] 225. Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  4. SpringBoot读取properties中的属性值

    1.在application.properties中添加以下内容: learn.blog.name=hello learn.blog.title=千回教育系统 2.新增属性关联的类: package ...

  5. 「LOJ2091」「ZJOI2016」小星星 容斥+DP

    题目描述 小 Y 是一个心灵手巧的女孩子,她喜欢手工制作一些小饰品.她有\(n\)颗小星星,用 \(m\)条彩色的细线串了起来,每条细线连着两颗小星星.有一天她发现,她的饰品被破坏了,很多细线都被拆掉 ...

  6. js继承的几种方法理解和代码演示

    1.属性继承 :call .apply:不建议使用浪费内存. function Person(name,age,sex){ this.name = name; this.age = age; this ...

  7. [转帖]Helm V2 迁移到 V3 版本

    Helm V2 迁移到 V3 版本 -- :: Mr-Liuqx 阅读数 63更多 分类专栏: kubernetes 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上 ...

  8. [转帖]GNU, Free Software and Open Source 自由软件与开源软件

    GNU, Free Software and Open Source 自由软件与开源软件 https://blog.csdn.net/icycolawater/article/details/7792 ...

  9. 安装donkeyid

    cd /usr/local/php/include/php/ext sudo git clone https://github.com/osgochina/donkeyid.git cd /usr/l ...

  10. Hadoop 系列(六)—— HDFS 常用 Shell 命令

    一.基本命令 打开 Hbase Shell: # hbase shell 1.1 获取帮助 # 获取帮助 help # 获取命令的详细信息 help 'status' 1.2 查看服务器状态 stat ...