目录
vector
构造函数
int main() {
vector<int> test;
test.push_back(1);
test.push_back(2);
test.push_back(3);
test.push_back(4);
test.push_back(5);
vector<int> tmp(test.begin(), test.end());
for (auto e : tmp) {
cout << e << " ";
}
return 0;
}
成员函数的代码实现和易错点
resize, reserve
template<class T>
class vector {
public:
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
//get value
size_t capacity() const {
return _end - _begin;
}
size_t size() const {
return _finish - _begin;
}
void reserve(size_t n) {
if (n > capacity()) {
value_type* tmp = new value_type[n];
size_t len = size();
if (_begin) {
//memcpy(tmp, _begin, sizeof(T) * capacity()); //memcpy这里不能用,自定义类型,会产生浅拷贝
for (int i = 0; i < size(); i++) {
tmp[i] = _begin[i];
}
delete[] _begin;
}
_begin = tmp;
_finish = _begin + len;
_end = _begin + n;
}
}
void resize(size_t n, value_type val = value_type()) {
if (n > capacity()) {
reserve(n);
}
if (n > size())
{
while (_finish < _begin + n) {
*_finish = val;
++_finish;
}
}
else {
_finish = _begin + n;
}
}
private:
iterator _begin;
iterator _finish;
iterator _end;
};
iterator
iterator begin() {
return _begin;
}
iterator end() {
return _finish;
}
const_iterator begin() const {
return _begin;
}
const_iterator end() const {
return _finish;
}
[ ]与at
迭代器失效
void insert(iterator pos, const value_type& val) {
assert(pos >= _begin);
assert(pos < _finish);
if (_finish == _end)
{
size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
reserve(newcapacity);
}
iterator end = _finish;
while (end >= pos) {
*end = *(end - 1);
end--;
}
*pos = val;
++_finish;
}
void insert(iterator pos, const value_type& val) {
assert(pos >= _begin);
assert(pos < _finish);
if (_finish == _end)
{ //扩容会导致迭代器失效
size_t len = pos - _begin; //这一步的目的是防止迭代器失效
size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
reserve(newcapacity);
pos = _begin + len;
}
iterator end = _finish;
while (end >= pos) {
*end = *(end - 1);
end--;
}
*pos = val;
++_finish;
}
list
list的整体框架
template<class T>
struct list_node {
list_node<T>* previ;
list_node<T>* next;
T val;
list_node(const T& x)
:previ(nullptr),
next(nullptr),
val(x)
{}
};
template <class T>
class list {
public:
typedef list_node<T> node;
void initialize_list() {
_head = new node(T());
_head->previ = _head;
_head->next = _head;
_size = 0;
}
/* constructor */
list()
:_head(nullptr)
{
initialize_list();
}
list(const list<T>& x)
:_head(nullptr)
{
initialize_list();
list<T> tmp(x.begin(), x.end());
swap(tmp);
}
/* capacity */
bool empty() {
return _size <= 0;
}
size_t size() {
return _size;
}
private:
node* _head;
size_t _size = 0;
};
list的迭代器
template<class T>
struct list_iterator {
typedef list_node<T> node;
typedef list_iterator<T> iterator;
node* it = nullptr;
list_iterator(node* x) :it(x){}
list_iterator(const iterator &x) :it(x.it){}
T& operator*() {
return it->val;
}
iterator& operator++() {
it = it->next;
return *this;
}
iterator& operator--() {
it = it->previ;
return *this;
}
bool operator!=(const iterator& x) const {
return it != x.it;
}
};
const T& operator*() {
return it->val;
}
T& operator*() const {
return it->val;
}
template<class T>
struct list_iterator {
typedef list_node<T> node;
typedef list_iterator<T> iterator;
node* it = nullptr;
list_iterator(node* x) :it(x){}
list_iterator(const iterator &x) :it(x.it){}
T& operator*() {
return it->val;
}
iterator& operator++() {
it = it->next;
return *this;
}
iterator& operator--() {
it = it->previ;
return *this;
}
bool operator!=(const iterator& x) const {
return it != x.it;
}
};
template<class T>
struct const_list_iterator {
typedef list_node<T> node;
typedef list_iterator<T> iterator;
node* it = nullptr;
const_list_iterator(node* x) :it(x){}
const_list_iterator(const iterator &x) :it(x.it){}
const T& operator*() {
return it->val;
}
iterator& operator++() {
it = it->next;
return *this;
}
iterator& operator--() {
it = it->previ;
return *this;
}
bool operator!=(const iterator& x) const {
return it != x.it;
}
};
template<class T, class ref>
struct list_iterator {
typedef list_node<T> node;
typedef list_iterator<T, ref> iterator;
node* it = nullptr;
list_iterator(node* x) :it(x){}
list_iterator(const iterator &x) :it(x.it){}
ref operator*() {
return it->val;
}
iterator& operator++() {
it = it->next;
return *this;
}
iterator& operator--() {
it = it->previ;
return *this;
}
bool operator!=(const iterator& x) const {
return it != x.it;
}
};
template <class T>
class list {
public:
typedef list_node<T> node;
//这里将两个版本的迭代器换成易懂的名称
typedef list_iterator<T, T&> iterator;
typedef list_iterator<T, const T&> const_iterator;
void initialize_list() {
_head = new node(T());
_head->previ = _head;
_head->next = _head;
_size = 0;
}
/* constructor */
list()
:_head(nullptr)
{
initialize_list();
}
list(const list<T>& x)
:_head(nullptr)
{
initialize_list();
list<T> tmp(x.begin(), x.end());
swap(tmp);
}
/* iterator */
//调用const或非const迭代器函数,会实例化出对应的迭代器类
iterator begin() const{
return iterator(_head->next);
}
const_iterator cbegin() const {
return const_iterator(_head->next);
}
iterator end() const{
return iterator(_head);
}
const_iterator cend() const {
return const_iterator(_head);
}
/* capacity */
bool empty() {
return _size <= 0;
}
size_t size() {
return _size;
}
private:
node* _head;
size_t _size = 0;
};
struct coor {
int _x;
int _y;
coor(int x = 0, int y = 0)
:_x(x),
_y(y){}
};
//假设已经包含了我们自己实现的头文件的类
//下面我调用的类是我们上面实现过的
int main() {
list<coor> test;
//假设已经实现过了push_back
test.push_back(coor(1,1));
test.push_back(coor(2,2));
auto it = test.begin();
std::cout << (*it)._x << (*it)._y <<endl;
return 0;
}
T* operator->() {
return &(it->val);
}
//假设已经包含了我们自己实现的头文件的类
//下面我调用的类是我们上面实现过的
int main() {
list<coor> test;
//假设已经实现过了push_back
test.push_back(coor(1,1));
test.push_back(coor(2,2));
auto it = test.begin();
std::cout << it->_x << it->_y <<endl;
return 0;
}
template<class T, class ref, class ptr>
struct list_iterator {
typedef list_node<T> node;
typedef list_iterator<T, ref, ptr> iterator;
node* it = nullptr;
list_iterator(node* x) :it(x){}
list_iterator(const iterator &x) :it(x.it){}
ref operator*() {
return it->val;
}
ptr operator->() {
return &(it->val);
}
iterator& operator++() {
it = it->next;
return *this;
}
iterator& operator--() {
it = it->previ;
return *this;
}
bool operator!=(const iterator& x) const {
return it != x.it;
}
};
template <class T>
class list {
public:
typedef list_node<T> node;
//这里将两个版本的迭代器换成易懂的名称
typedef list_iterator<T, T&, T*> iterator;
typedef list_iterator<T, const T&, const T*> const_iterator;
void initialize_list() {
_head = new node(T());
_head->previ = _head;
_head->next = _head;
_size = 0;
}
/* constructor */
list()
:_head(nullptr)
{
initialize_list();
}
list(const list<T>& x)
:_head(nullptr)
{
initialize_list();
list<T> tmp(x.begin(), x.end());
swap(tmp);
}
/* iterator */
//调用const或非const迭代器函数,会实例化出对应的迭代器类
iterator begin() const{
return iterator(_head->next);
}
const_iterator cbegin() const {
return const_iterator(_head->next);
}
iterator end() const{
return iterator(_head);
}
const_iterator cend() const {
return const_iterator(_head);
}
/* capacity */
bool empty() {
return _size <= 0;
}
size_t size() {
return _size;
}
private:
node* _head;
size_t _size = 0;
};
无法将非const迭代器赋给const迭代器
template<class T, class ref , class ptr>
struct list_iterator {
//这里一定要声明出iterator
//不然我们在自己实现拷贝构造时编译器不认识 iterator
typedef list_iterator<T, T&, T*> iterator;
typedef list_iterator<T, const T&, const T*> const_iterator;
typedef list_iterator<T, ref, ptr> self;
typedef list_node<T> node;
node* it = nullptr;
list_iterator(node* x) :it(x){}
list_iterator(const iterator &x) :it(x.it){}
ref operator*() {
return it->val;
}
ptr operator->() {
return &it->val;
}
self& operator++() {
it = it->next;
return *this;
}
self& operator--() {
it = it->previ;
return *this;
}
self& operator++(int) {
self tmp = *this;
it = it->next;
return tmp;
}
self& operator--(int) {
self tmp = *this;
it = it->previ;
return tmp;
}
bool operator!=(const self& x) const {
return it != x.it;
}
};
反向迭代器
template<class Iterator, class ref, class ptr>
struct reverse_iterator {
typedef reverse_iterator<Iterator, ref, ptr> self;
reverse_iterator(const Iterator& x) :_con(x) {}
reverse_iterator(const reverse_iterator& x) :_con(x._con) {}
ref operator*() {
Iterator tmp = _con;
return *(--tmp);
}
ptr operator->() {
return &(operator*());
}
self& operator++() {
--_con;
return *this;
}
self& operator++(int) {
self tmp = _con;
--_con;
return tmp;
}
self& operator--() {
++_con;
return *this;
}
self& operator--(int) {
self tmp = _con;
++_con;
return tmp;
}
bool operator!=(const self& x) {
return _con != x._con;
}
private:
Iterator _con;
};