vector的自定义实现
#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<complex>
#include<new>
#include<memory>
#include<exception>
#include<cstdlib>
#include<iterator>
#include<initializer_list>
using namespace std;
//template<typename t> class alloc
//{public:
// alloc():a(),s(){ //成员变量默认初始化
// }
// t* allocate(size_t n){
// return a.allocate(n);
// }
////变长模板参数,allocator的第二个参数是变长的,要调用它,外层的construct也要有变长参数
//template<typename...Args> void construct(t* p, const Args&...rest){
// a.construct(p,rest... );
// }
// void deallocate(t* p, size_t n)
// {
// a.deallocate(p, n);
// }
// void destory(t* p)
// {
// a.destory(p);
// }
//
//
//private:
// allocator<t> a;
// string s;
//
//};
//vector为空,capacity=0,否则预分配空间为16,当空间不足时,扩大两倍增长,
template<typename T, class Allocator = std::allocator<T> >
class Vector{
private:
T* start;
T* finish;
T* end_of_storage;
size_t length;
size_t capacity;
public:
typedef T* iterator;
typedef const T* const_iterator;
Vector():start(NULL),finish(){
end_of_storage = NULL;
length = ;
capacity = ;
}
Vector(const std::initializer_list<T> &lst)
{
for (auto it = lst.begin(); it != lst.end();it++)
{
this->push_back(*it);
} }
Vector(size_t n, const T&value = T()){
Allocator a;
T*p = NULL;
if (n <= ){ p = a.allocate();
capacity = ;
}
else {
p = a.allocate(static_cast<int>(1.5 * n));
capacity = static_cast<size_t>(1.5*n);
}
start = p;
finish=uninitialized_fill_n(p, n, value);
if (n <= ) end_of_storage = start + ;
else end_of_storage = p + static_cast<int>(1.5*n)-;
length = n;
}
/*Vector(size_t n, const T&value){ Vector(n, value); }*/
Vector(const Vector& rhs){
capacity = rhs.capacity;
length = rhs.length;
Allocator a;
start=a.allocate(capacity);
end_of_storage = start + capacity - ;
finish = start + length ; for (T* p = start,*q = rhs.start; p != finish; p++, q++){
a.construct(p, *q);
}
}
void push_back(const T& value){
if (capacity == ){ //为空容器时需要申请新空间
Allocator a;
start = a.allocate();
a.construct(start, value);
finish = start + ;
end_of_storage = start + ;
length = ;
capacity = ;
}
else if (length < capacity){
Allocator a;
a.construct(finish++, value);
length += ;
}
else { //空间不足时1.5倍扩大空间
Allocator a;
auto old_capacity = capacity;
auto new_start = a.allocate(static_cast<size_t>(1.5*capacity));
auto new_finish = uninitialized_copy(this->begin(), this->begin()+length, new_start); //没有使用迭代器形式
new_finish = uninitialized_fill_n(new_finish, , value);
//释放掉旧空间
capacity = static_cast<size_t>(1.5*capacity);
length += ; auto temp = start;
while (temp != finish)
{
a.destroy(temp++);
}
a.deallocate(start, old_capacity);
start = new_start;
finish = new_finish;
end_of_storage = start + capacity - ;
}
}
void pop_back(){//弹出的意义是不在含有该对象
Allocator a;
a.destroy(--finish); //finish指向尾后位置,自减后销毁了对象,不存在了
length -= ; }
iterator insert(const_iterator position, const T&value);
T& operator[](size_t n){return *(start + n); } //如果index out of bound or Vector为空 ,让用户自己去处理
const T& operator[](size_t n)const { return *(start + n); }
T& front(){ return *start; }
const T&front()const{ return *start; }
T& back(){ return *(finish-); }
const T&back()const{ return *(finish - ); }
size_t size(){ return length; }
size_t volume(){ return capacity; }
iterator begin(){ return start; }
const_iterator cbegin()const { return start; }
iterator end(){ return finish; }
const_iterator cend(){ return finish; }
Vector& operator=(const Vector&rhs){
if (this->start ==rhs.start) return *this; //两个相等,两个相同
//释放掉原有的内存空间,allocator分配的空间如果不deallocate,会造成内存泄漏
auto old_start = this->start;
auto old_finish = this->finish;
size_t old_capacity = this->capacity;
capacity = rhs.capacity;
length = rhs.length;
Allocator a;
start = a.allocate(capacity);
end_of_storage = start + capacity - ;
finish = start + length; for (T* p = start, *q = rhs.start; p != finish; p++, q++){
a.construct(p, *q);
}
Allocator b;//是需要重新创建b,还是用前面的a就可以完成下面的动作
T* p = old_start;
while (p!=old_finish)
{
b.destroy(p++);
}
b.deallocate(old_start, old_capacity);
return *this; };
~Vector(){
Allocator a;
T* p = start;
while (p!= finish)
{
a.destroy(p++);
}
a.deallocate(start, capacity);
start = finish = end_of_storage = NULL; //是否多余语句
cout << " dectr completed";
} };
template<typename T>
class demo{
public:
demo():d(){}
T size();
private:
T d;
};
class foo{
public:
foo() :a(), s("dka"){}
private:
int a; string s; }; int main()
{
Vector<string> v1;
cout << v1.volume() << " " << v1.size() << "\n";
Vector<string> v2();//10个空串,调用了string的default ctr
cout << v2.size() << " " << v2.volume() << endl;
cout << "the first item of v2: " << v2[] << " or " << v2.front() << endl;
//Vector<string> v3(10, 'c'); no instance of parameters (int,char)
Vector<string> v4(, "c");
Vector<string> v5(v4);
cout << v5.back() << " " << v5.size() << v5.volume() << endl;
v5 = v1;
for (auto it = v5.begin(); it!= v5.end();it++)
{
cout << *it;
}
/*cout << v5[0] << endl;*/ Vector<string> v6(v1); //用空vector初始化另一个vector ;这两个vector都应该表现正常
for (auto it = v6.cbegin(); it != v6.cend();it++)
{
cout << *it << endl;
}
v1 = v4;//v1原本为空,用含10个元素的v4赋值,并用push_back添加尾元素
for (auto x : v1) cout << x << endl;
v1.push_back("hdwa");
cout << v1.size() << v1[] << endl;
v1.pop_back();
cout << v1.back() << endl;
string s;
Vector<int> iv();
for (auto y:iv)
{
printf("%d", y);
}
Vector<string> v7={ "dak", "dj" };
cout << v7.volume() << " " << v7.size() << v7[] << " " << v7[] << endl; }
vector的自定义实现的更多相关文章
- C++ Vector 中自定义对象的排序
需求: 客户端收到游戏中的所有联盟列表,现在需要按联盟的属性比如lv来进行排序. 数据存储: 每个联盟数据是一个对象,所有的联盟列表存在一个vector容器里面. 老的解决方法: 冒泡排序方法算法 新 ...
- std list/vector sort 自定义类的排序就是这么简单
所以,自己研究了一下,如下:三种方式都可以,如重写<,()和写比较函数compare_index.但是要注意对象和对象指针的排序区别. 1.容器中是对象时,用操作符<或者比较函数,比较函数 ...
- 【转】c++中Vector等STL容器的自定义排序
如果要自己定义STL容器的元素类最好满足STL容器对元素的要求 必须要求: 1.Copy构造函数 2.赋值=操作符 3.能够销毁对象的析构函数 另外: 1. ...
- C++ STL vector容器学习
STL(Standard Template Library)标准模板库是C++最重要的组成部分,它提供了一组表示容器.迭代器.函数对象和算法的模板.其中容器是存储类型相同的数据的结构(如vector, ...
- C++ vector,list,deque区别(转)
在写C++程序的时候会发现STL是一个不错的东西,减少了代码量,使代码的复用率大大提高,减轻了程序猿的负担.还有一个就是容器,你会发现要是自己写一个链表.队列,或者是数组的时候,既要花时间还要操心 ...
- C++ vector、list和deque的区别 (整理)
1.vector数据结构 vector和数组类似,拥有一段连续的内存空间,并且起始地址不变.因此能高效的进行随机存取,时间复杂度为o(1);但因为内存空间是连续的,所以在进行插入和删除操作时,会造成内 ...
- 深入Collection集合
List集合 一.ArraryList: 最基本的集合不多做介绍 二.Vector Vector cn=new Vector(); A:有特有功能 a:添加 public void ad ...
- 从yield关键字看IEnumerable和Collection的区别
C#的yield关键字由来以久,如果我没有记错的话,应该是在C# 2.0中被引入的.相信大家此关键字的用法已经了然于胸,很多人也了解yield背后的“延迟赋值”机制.但是即使你知道这个机制,你也很容易 ...
- java12
1:List的子类(掌握) (1)List的子类特点 ArrayList: 底层数据结构是数组,查询快,增删慢 线程不安全,效率高 Vector: 底层数据结构是数组,查询快,增删慢 线程安全,效率低 ...
随机推荐
- Mysql 实现基于binlog的主从同步
工作原理 1.主节点必须启用二进制日志,记录任何修改了数据库数据的事件.2.从节点开启一个线程(I/O Thread)把自己扮演成 mysql 的客户端,通过 mysql 协议,请求主节点的二进制日志 ...
- spring security简单教程以及实现完全前后端分离
spring security是spring家族的一个安全框架,入门简单.对比shiro,它自带登录页面,自动完成登录操作.权限过滤时支持http方法过滤. 在新手入门使用时,只需要简单的配置,即可实 ...
- Codeforces 1172B(组合数学)
题面 给出一棵n个点的树,要求把它画在圆上,且边不相交,画法与排列一一对应(即旋转后相同的算不同种),求方案数.如下图是4个点的树\(T:V=\{1,2,3,4\},E=\{(1,2),(1,3),( ...
- 物流运输(最短路+dp)
这道题是相当的火,但是在tyher的讲解下我一遍就AC了!!! Part 1 理解题目 从第一天到最后一天,总会有一些点莫名其妙地走不了,所以导致我们不能按照上一次的最短路一直运输得到最少费用,而需要 ...
- 7、numpy——广播
1.广播的引出 广播(Broadcast)是 numpy 对不同形状(shape)的数组进行数值计算的方式, 对数组的算术运算通常在相应的元素上进行. 如果两个数组 a 和 b 形状相同,即满足 a. ...
- 标签的增加、删除与复制,动态标签js不生效的解决
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- asp.net ajax的使用
参考:https://www.cnblogs.com/acles/articles/2385648.html https://www.cnblogs.com/xujingyang/p/5560646. ...
- Windows程序设计--(二)Unicode 简介
2.2 宽字符和C语言 2.2.2 更宽的字符 在C语言中的宽字符正是基于short型数据的, 这一数据类型在头文件WCHAR.H中的定义为: typedef unsigned short wchar ...
- 联想笔记本 thinkpad BIOS 超级密码 Supervisor Password 清除 破解 亲测有效 转载地址https://blog.csdn.net/ot512csdn/article/details/72571674
联想笔记本 thinkpad BIOS 超级密码 Supervisor Password 清除 破解 亲测有效 转载地址https://blog.csdn.net/ot512csdn/article/ ...
- 接口需要上一个接口的返回值(unittest)
import unittest,requests ''' 使用unittest框架的时候,这个接口需要上一个接口的返回值 ''' class Test_case(unittest.TestCase): ...