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: 底层数据结构是数组,查询快,增删慢 线程安全,效率低 ...
随机推荐
- spring容器的启动过程
spring的启动过程: 首先,对于一个web应用,其部署在web容器中,web容器提供其一个全局的上下文环境,这个上下文就是ServletContext,其为后面的spring IoC容器提供宿主环 ...
- (转载)Solr4.x在Tomcat下的部署
Step1 下载安装包: 下载最新版本安装包 点击此处下载Tomcat 点击此处下载Solr Step2 解压: 解压Tomcat和Solr Step3 拷贝War包: 拷贝\solr-4.x\ ...
- python学习二十四天函数参数之默认参数
函数参数就是向函数传递参数,可以传递一个,可以是更多个,有的参数有值,有的没有,函数可以设置默认参数,默认参数必须放参数最后面. 1,不传递参数,设置默认参数 def hello(a,b,c='123 ...
- python 列表总结大全
1定义 names=[] names=[1,2,1,1,1,] names=[1.'10'.[1,1]] 2添加元素 names.append() names.insert(0,10) names.e ...
- go web编程——实现一个简单分页器
在go web编程中,当需要展示的列表数据太多时,不可避免需要分页展示,可以使用Go实现一个简单分页器,提供各个数据列表展示使用.具体需求:1. 可展示“首页”和“尾页”.2. 可展示“上一页”和“下 ...
- IIS故障 应用程序池“XXX”提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误。该进程 ID 为“XXXX”。数据字段包含错误号。
(尝试失败,但觉得有可行性) 参考https://www.cnblogs.com/qidian10/p/6028784.html https://yq.aliyun.com/articles/6434 ...
- Task总结
1.Task的优势 ThreadPool相比Thread来说具备了很多优势,但是ThreadPool却又存在一些使用上的不方便.比如: ◆ ThreadPool不支持线程的取消.完成.失败通知等交互性 ...
- xcode 5.0 连接svn error -(NSURLErrorDomain error -1012)
xcode 5.0连接 svn server, check out时出现如下error : The operation couldn’t be completed. (NSURLErrorDomain ...
- C语言获取当前时间
#include <stdio.h> #include <time.h> void main () { time_t rawtime; struct tm * timeinfo ...
- Vue:对象更改检测注意事项
还是由于 JavaScript 的限制,Vue 不能检测对象属性的添加或删除: var vm = new Vue({ data: { a: 1 } }) // `vm.a` 现在是响应式的 vm.b ...