C++实现Vector容器的基本功能
本文只实现了Vector的默认构造函数、赋值构造函数、赋值函数、析构函数、重置空间大小函数和插入函数,权当起到抛砖引玉的作用,其他函数功能的实现可在此基础之上进行拓展。
#include <iostream>
using namespace std; template <class T>
class Vector { public:
// 构造函数
Vector(int size=):theSize(size),theCapacity(size+SPACE_CAPACITY){
data = new T[theCapacity];
} // 复制构造函数
Vector(const Vector& other) :theSize(),theCapacity(),data(NULL){
*this=other;
}
// 重载赋值函数
Vector& operator=(Vector& other) {
// 判断是否为自身赋值
if (this == &other)
return *this;
else {
delete[]data;
theSize = other.theSize;
theCapacity = other.theCapacity;
data = new T[theCapacity]; for (int i = ; i < theSize; ++i) {
data[i] = other.data[i];
}
return *this;
}
}
// 析构函数
~Vector(void) {
delete[] data;
}
// 重新分配空间大小函数
void reServe(int newCapacity) {
if (newCapacity <= theCapacity)
return; T *temp = data;
data = new T[newCapacity];
for (int i = ; i < theSize; ++i)
data[i] = temp[i];
delete[] temp;
}
// push_back函数
void push_back(T val) {
if (theSize == theCapacity)
reServe( * theCapacity + );
data[theSize++] = val;
}
private:
const int SPACE_CAPACITY = ;
int theCapacity;
int theSize;
T *data;
};
C++实现Vector容器的基本功能的更多相关文章
- STL—— 容器(vector)的各种功能方法
1. 获取容器的元素个数 size() 使用 vectorName.size() 可以输出这个容器中类型的个数,如下代码: 1 #include <iostream> 2 #include ...
- vector容器的用法
转自一篇博客^-^: 1 基本操作 (1)头文件#include<vector>. (2)创建vector对象,vector<int> vec; (3)尾部插入数字:vec.p ...
- 标准模板库(STL)学习探究之vector容器
标准模板库(STL)学习探究之vector容器 C++ Vectors vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被 ...
- STL学习二:Vector容器
1.Vector容器简介 vector是将元素置于一个动态数组中加以管理的容器. vector可以随机存取元素(支持索引值直接存取, 用[]操作符或at()方法,这个等下会详讲). vector尾部添 ...
- STL学习系列二:Vector容器
1.Vector容器简介 vector是将元素置于一个动态数组中加以管理的容器. vector可以随机存取元素(支持索引值直接存取, 用[]操作符或at()方法,这个等下会详讲). vector尾部添 ...
- C++中vector 容器的基本操作
vector是一种简单高效的容器,具有自动内存管理功能.对于大小为n的vector容器,它的元素下标是0~n-1. vector有二个重要方法: begin(): 返回首元素位置的迭代器. ...
- 带你深入理解STL之Vector容器
C++内置了数组的类型,在使用数组的时候,必须指定数组的长度,一旦配置了就不能改变了,通常我们的做法是:尽量配置一个大的空间,以免不够用,这样做的缺点是比较浪费空间,预估空间不当会引起很多不便. ST ...
- STL - vector容器
1Vector容器简介 vector是将元素置于一个动态数组中加以管理的容器. vector可以随机存取元素(支持索引值直接存取, 用[]操作符或at()方法,这个等下会详讲). vector尾部添加 ...
- (转载)C++STL中vector容器的用法
vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说vec ...
随机推荐
- QTQuick控件基础(2)
import QtQuick 2.2import QtQuick.Controls 1.2import QtQuick.Window 2.1ApplicationWindow { visible ...
- Python3基础 str endswith 是否以指定字符串结束
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- mongodb 有一个坑 报错 no mongos proxies found in seed list
mongoose 的报当我从 mongoose@4.5.2 升级到 mongoose@4.6.5的时候,出现了一个问题: Unhandled rejection MongoError: no mong ...
- hdu 3698 UVA1490 Let the light guide us 线段树优化DP
题目链接 and 题目大意 hdu3698 但是 hdu的数据比较弱,所以在这luogu提交吧UVA1490 Let the light guide us 有一个\(n*m\)的平原,要求每行选一个点 ...
- H5本地存储一
localStorage(本地存储),可以长期存储数据,没有时间限制,一天,一年,两年甚至更长,数据都可以使用.sessionStorage(会话存储),只有在浏览器被关闭之前使用,创建另一个页面时同 ...
- C# 读取txt文件内容
if (!System.IO.File.Exists(@"E:\\111.txt")) { Console.Write("没有找到文件!"); } System ...
- TCGA系列--LncMAP
LncMAP:http://www.bio-bigdata.com/LncMAP/index.jsp
- 华中农业大学第四届程序设计大赛网络同步赛 G.Array C 线段树或者优先队列
Problem G: Array C Time Limit: 1 Sec Memory Limit: 128 MB Description Giving two integers and and ...
- Jmeter测试API接口,用Jmeter自动化之检查DB数据
如上: 注册接口,会新增数据,要怎么自动化检查DB中生成的数据呢? 很简单,只需要以下几个配置元件 JSON截取器或者正则表达式截取器:目的在于取出返回消息体中的数据aa JDBC后置处理器:目的在于 ...
- SQL实现新增表,表名更改,列名更改,约束更改等
--新建表: CREATE TABLE TABLENAME ( ID INT IDENTITY (1,1) PRIMARY KEY , NAME VARCHAR(50) DEFAULT 'HELLO' ...