添加vector容器

This commit is contained in:
邹晓航
2014-09-20 11:19:53 +08:00
parent 09ec83a740
commit 8e9b7b611e
2 changed files with 123 additions and 10 deletions

115
TinySTL/Vector.h Normal file
View File

@@ -0,0 +1,115 @@
#ifndef _VECTOR_H_
#define _VECTOR_H_
#include "Allocator.h"
#include "Iterator.h"
#include "UninitializedFunctions.h"
namespace TinySTL{
namespace {
template<class T>
class viter: public iterator<random_access_iterator<T, ptrdiff_t>, T>{
private:
T * ptr_;
public:
viter() :ptr_(0){}
explicit viter(T *ptr):ptr_(ptr){}
viter(const viter& vit);
viter& operator = (const viter& vit);
T& operator *(){ return *ptr_; }
T *operator ->(){ return &(operator *()); }
viter& operator ++(){ ++ptr_; return *this; }
viter operator ++(int){ viter temp = *this; ++(*this); return temp; }
viter& operator --(){ --ptr_; return *this; }
viter operator --(int){ viter temp = *this; --(*this); return temp; }
bool operator == (const viter& vit){ return ptr_ == vit.ptr_; }
bool operator != (const viter& vit){ return !(*this == vit); }
typename viter::difference_type operator - (const viter& vit){ return ptr_ - vit.ptr_; }
friend typename viter::difference_type operator - (const viter& left, const viter& right);
};
template<class T>
viter<T>::viter(const viter& vit){
ptr_ = vit.ptr_;
}
template<class T>
viter<T>& viter<T>::operator = (const viter& vit){
if (this != &vit){
ptr_ = vit.ptr_;
}
}
template<class T>
typename viter<T>::difference_type operator - (const viter<T>& left, const viter<T>& right){
return left - right;
}
}// end of anonymous namespace
template<class T, class Alloc = allocator<T>>
class vector{
private:
T *start_;
T *finish_;
T *endOfStorage_;
typedef Alloc dataAllocator;
//Alloc dataAllocator;
public:
typedef T value_type;
typedef viter<T> iterator;
typedef iterator pointer;
typedef T& reference;
typedef size_t size_type;
typedef typename iterator::difference_type difference_type;
public:
//<2F><><EFBFBD><EFBFBD><ECA3AC><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>غ<EFBFBD><D8BA><EFBFBD>
vector()
:start_(0), finish_(0), endOfStorage_(0){}
explicit vector(const size_type n);
vector(const size_type n, const value_type& value);
template<class InputIterator>
vector(InputIterator first, InputIterator last);
vector(const vector& v);
vector(vector&& v);
vector& operator = (const vector& v);
~vector(){
dataAllocator::destroy(start_, finish_);
dataAllocator::deallocate(start_, endOfStorage_ - start_);
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
iterator begin(){ return iterator(start_); }
iterator end(){ return iterator(finish_); }
private:
void allocateAndFillN(const size_type n, const value_type& value){
start_ = dataAllocator::allocate(n);
uninitialized_fill_n(start_, n, value);
finish_ = endOfStorage_ = start_ + n;
}
template<class InputIterator>
void allocateAndCopy(InputIterator first, InputIterator last){
start_ = dataAllocator::allocate(last - first);
finish_ = uninitialized_copy(first, last, start_);
endOfStorage_ = finish_;
}
};
template<class T, class Alloc>
vector<T, Alloc>::vector(const size_type n){
allocateAndFillN(n, value_type());
}
template<class T, class Alloc>
vector<T, Alloc>::vector(const size_type n, const value_type& value){
allocateAndFillN(n, value);
}
template<class T, class Alloc>
template <class InputIterator>
vector<T, Alloc>::vector(InputIterator first, InputIterator last){
allocateAndCopy(first, last);
}
}
#endif

View File

@@ -1,5 +1,7 @@
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include "Allocator.h"
#include "Construct.h"
@@ -9,16 +11,12 @@
using namespace std;
int main(){
/*for (int i = 1; i != 100000; ++i){
auto p = TinySTL::allocator<int>::allocate();
}*/
auto p = TinySTL::allocator<int>::allocate(100);
TinySTL::uninitialized_fill(p, p + 100, 88);
auto last = p + 100;
int array[100];
TinySTL::uninitialized_fill_n(array, 100, 88);
for (auto n : array){ cout << n << endl; }
int array[3] = { 1, 2, 3 };
TinySTL::vector<int> vec(array, array + 3);
//TinySTL::vector<int> vec(3, 1); -> error C2019
cout << *(vec.begin()) << endl;
cout << *(++vec.begin()) << endl;
cout << *(--vec.end()) << endl;
system("pause");
return 0;
}