Skip to content

Instantly share code, notes, and snippets.

@fateyan
Created Mar 14, 2021
Embed
What would you like to do?
#ifndef _FY_STL_VECTOR
#define _FY_STL_VECTOR
#include <iostream>
#include <cstddef>
namespace fateyan {
template <typename T>
class Vector {
public:
class iterator {
public:
typedef std::random_access_iterator_tag iterator_category;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef int difference_type;
iterator(): ptr(NULL) {;}
iterator(T* vec): ptr(vec) {;}
iterator(const iterator& it) {
ptr = it.ptr;
}
iterator& operator ++ () { ptr++; return *this; }
iterator& operator ++ (int) { ptr++; return *this; }
iterator& operator -- () { ptr--; return *this; }
iterator& operator -- (int) { ptr--; return *this; }
iterator operator + (size_t diff) { return iterator(ptr + diff); }
difference_type operator + (iterator other) { return (ptr + other.ptr); }
iterator operator - (size_t diff) { return (ptr - diff); }
difference_type operator - (iterator other) { return (ptr - other.ptr); }
bool operator == (const iterator& it) { return (ptr == it.ptr); }
bool operator != (const iterator& it) { return (ptr != it.ptr); }
bool operator < (const iterator& it) { return (*this - it < 0); }
T& operator * () { return *ptr; }
T*& operator -> () { return ptr; }
friend class Vector<T>;
private:
T* ptr;
};
Vector(): _size(0), _capacity(16), _content(new T[16]) {;}
Vector(size_t count): _size(count), _capacity(count), _content(new T[count]) {;}
Vector(const Vector& source): _size(0), _capacity(source._size), _content(new T[source._size]) {
while (_size < source._size)
_content[_size++] = source[_size];
}
Vector(size_t count, const T defaultValue): Vector(count) {
for (size_t i = 0; i < count; ++i)
_content[i] = defaultValue;
}
template<typename InputIt>
Vector(InputIt first, InputIt last) {
int i = 0;
while((first+i) != last) {
_content[i++] = first.ptr;
}
}
virtual ~Vector() { delete [] _content; }
T& operator [] (const int idx) { return _content[idx]; }
const T& operator [] (const int idx) const { return _content[idx]; }
T& front() { return *_content; }
T& back() { return _content[_size-1]; }
iterator begin() const { return iterator(_content); }
iterator end() const { return iterator(_content + _size); }
bool empty() const { return (_size == 0); }
size_t size() const { return _size; }
size_t capacity() const { return _capacity; }
void clear() { _size = 0; }
void insert(iterator pos, const T& value) {
if(_size == _capacity) resize(_capacity << 1);
int i;
for(i = _size-1; i >= 0; --i) {
_content[i+1] = _content[i];
if(pos == &_content[i]) break;
}
_content[i] = value;
++_size;
}
template <typename InputIt>
void insert(iterator pos, InputIt first, InputIt last) {
while(first != last) insert(pos, *first++);
}
void erase(iterator it) { erase(it, it+1); }
void erase(iterator first, iterator last) {
while(first != last) {
*first = *last;
++first; ++last;
}
_size = first.ptr - begin().ptr;
}
void push_back(const T& value) {
if (_size >= _capacity)
resize(_capacity << 1);
_content[_size++] = value;
}
void pop_back() { --_size; }
void resize(size_t sz) {
if(_size >= sz) {
_size = sz;
return;
}
if(_capacity < sz) {
_capacity = sz;
T *temp = _content;
_content = new T[_capacity];
for(size_t i = 0; i < sz; ++i)
_content[i] = temp[i];
delete [] temp;
}
}
private:
T *_content;
size_t _size;
size_t _capacity;
};
template <class t> struct greater {
bool operator () (const t& x, const t& y) const {return x>y;}
};
template <class t> struct less {
bool operator () (const t& x, const t& y) const {return x<y;}
};
template <typename T>
void swap(T& a, T& b) {
T temp = a;
a = b;
b = temp;
}
template <typename RandomIt, typename Compare>
void sort(RandomIt first, RandomIt last, Compare cmp) {
size_t len = last - first;
for(int i = 0; i < len; ++i)
for(int j = 0; j < len; ++j)
if(cmp(*(first+i), *(first+j))) {
swap(*(first+i), *(first+j));
}
}
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment