STL 之 list源码自行实现(iterator)

Posted jzssuanfa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STL 之 list源码自行实现(iterator)相关的知识,希望对你有一定的参考价值。

(0)文件夹

STL 之 vector源码实现(云算法<< [] = 重载, new delete,throw catch)

STL  c++中string类的源码

堆(stack) 之 c 和 c++模板实现(空类默认成员函数 初谈引用 内联函数)

第一次实现list模板(幼稚的我)

浅析STL 谓词 + 仿函数 + 函数指针(c)

队列(queue) 之 c++模板实现(友元函数和运算符重载)

STL 之 初识set multiset(map multimap)

C++map类型 之 简单介绍

c++ string 之 find_first of 实现(更改篇)

泛型算法 —— 独立于容器的算法

初识迭代器 C++ Iterator

哈希(hash) 之插入和查找(链地址法)

c++ fstream + string 处理大数据(与c 的fread)

c++中的悬浮指针和野指针

二级指针

一:起因(參考)

(1)数据结构里面两种很重要的存储结构,线性结构中的连续存储结构(代表vector数组)和非连续存储结构(代表list链表),他们两者被广泛的应用在

各个领域。是最基本最基础的两种存储结构。

(2)vector 已经简单的实现了,请看STL 之 vector的实现     之前还实现了STL的string类,请看 STL 之 string 的实现

(3)之前的友元类仅仅是停留在理论层面,真正实现还是头一次。(尽管友元函数,无数次的实现过了)这个友元类正是 Iterator 类,也是初次实现

Iterator。见识了Iterator ++ --运算符的重载,注意只实现了单一方向的++ --(这是源码这样处理的)。友元类很像java中的内部类。

(4)我也幼稚过,请看第一次实现list模板

(5)list 自己也是结合STL的源码,结合数据结构简单的实现的。里面可能有一些处理不当,请大神们指正,谢谢。

二:list实现

(1)list的分析

List是一种可在常数时间内在不论什么位置运行插入和删除操作的顺序容器。

list是双向链表。其迭代器是双向的。与其它顺序容器(array, vector, deque)相比。list

器在任何位置运行插入、提取、和移动元素的操作更高效,但它不能通过在容器中的位置直接获取元素。

(2)list中的一些主要函数(注意。自己只实现了一部分)

Operations

list::splice() ————  将一个list A中的元素转移到list B的指定位置。并将A中被转移的元素删除。



list::remove()  ————   将list中指定的元素删除。



list::unique() ———— 删除list中具有同样值的元素,仅仅保留第一个。也能够依据条件删除具有同样条件的元素,仅仅保留第一个。

list::merge() ————  合并两个list。在合并之前两个list应该先排序,合并之后的list依旧有序。

也能够自己定义排序的条件。

list::sort() ———— 对list中的元素进行排序,变更它们在容器中的位置。sort()还能够按给定条件进行排序。



list::reverse() ——_ 改变list中元素的顺序。


(3)list的实现

    a   构造函数 MyList(const MyList<T>& list0)

template<class T>
MyList<T>::MyList(const MyList<T>& list0)
{
    size = list0.size;
    node *cur0 = list0.head;
    node *tp = new node;
    tp->data = cur0->data;
    head = tp;
    rear = tp;
    head->last = NULL;
    while(cur0->next!=NULL)
    {
        cur0 = cur0->next;
        node *tp = new node;
        tp->data = cur0->data;
        tp->last = rear;
        rear->next = tp;
        rear = tp;
    }
    rear->next = NULL;

}
b  +=重载(注意自加)

// MyList 的+= 感觉能够改进的,别两个while了
        MyList<T>& operator += (const MyList<T>& list)
        {
            if(list.head == (*this).head)
            {// meichuli
                //cout << "****" << endl;
                MyList<T> other(list);
                //cout << "&&&&&" << endl;
                return *this += other;
            }
            if(NULL == list.head)// list 空串
                return *this;
            node *cur = list.head;
            node *tp = new node;
            tp->data = cur->data;
            if(head == NULL)
            {// 当前list为空的
                head = tp;
                head ->last = NULL;
                rear = tp;
            }
            else
            {
                rear->next = tp;
                tp->last = rear;
                rear = tp;
            }

            while(cur->next!=NULL)
            {
                cur = cur->next;
                node *tp = new node;
                tp->data = cur->data;
                rear->next = tp;
                tp->last = rear;
                rear = tp;
            }
            rear->next = NULL;
            size += list.size;
            return *this;
        }

c  友元类(内部类)

//Iterator class
    class MyIterator
    {
        friend class MyList<T>;
        protected:
            node* Iter;
            MyIterator(node *iter)
            {
                Iter = iter;
            }
        public:
            MyIterator(){}
            // 先加上
            MyIterator& operator ++()
            {
                Iter = Iter->next;
                return *this;
            }
            // 先减去
            MyIterator& operator --()
            {
                Iter = Iter->last;
                return *this;
            }
            MyIterator& operator = (MyIterator& it)
            {
                Iter = it.Iter;
                return *this;
            }
            bool operator ==(const MyIterator& iter)
            {
                return Iter==iter.Iter?true:false;
            }
            bool operator !=(const MyIterator& iter)
            {
                return Iter!=iter.Iter?true:false;
            }
            T operator *()
            {
                return Iter->data;
            }
            MyIterator &operator[](int n)
            {
                for(int i=0;i<n;i++)
                {
                    assert(NULL!=Iter);
                    Iter=Iter->next;
                }
                return *this;
            }
            MyIterator &operator +(int n)
            {
                for(int i=0;i<n;i++)
                {
                    assert(NULL!=Iter);
                    Iter=Iter->next;
                }
                return *this;
            }
    };// MyIterator 内部类

d  字符串反转

// reverse 对于同一个串进行交换。

。 template<class T> void MyList<T>::reserve(MyIterator& it1,MyIterator& it2) { node *le = it1.Iter; node *ri = it1.Iter; int c = 1; // 不是非常理解的 for(;ri!=it2.Iter;ri=ri->next) { c++; } for(;c>1;c-=2) { // 值交换,指针交换?? le->data ^= ri->data; ri->data ^= le->data; le->data ^= ri->data; le = le->next; ri = ri->last; } }

e   移除指定元素

// remove value
template<class T>
void MyList<T>::remove(T val)
{
    node *tp = head;
    for(;tp!=NULL;)
    {
        if(tp->data==val)
        {
            tp = this->erase(MyIterator(tp)).Iter;
        }
        else
        {
            tp = tp->next;
        }
    }
}

(4)完整代码例如以下:MyList.h

#ifndef MYLIST_H_INCLUDED
#define MYLIST_H_INCLUDED
#include <iostream>
#include <cassert>
#include <cstdio>
#include <string>
using namespace std;

template<class T>
class MyList
{
    friend class MyIterator;
    protected:
        struct node
        {
            T data;
            node *next;
            node *last;
        };
    private:
        node *head;
        node *rear;
        int size;
    public:
    //constructor
        MyList();
        MyList(const MyList<T>&);
        MyList(T,int);
        MyList(T);
    //disconstructor
        ~MyList(){this->clear();}
    //Iterator class
    class MyIterator
    {
        friend class MyList<T>;
        protected:
            node* Iter;
            MyIterator(node *iter)
            {
                Iter = iter;
            }
        public:
            MyIterator(){}
            // 先加上
            MyIterator& operator ++()
            {
                Iter = Iter->next;
                return *this;
            }
            // 先减去
            MyIterator& operator --()
            {
                Iter = Iter->last;
                return *this;
            }
            MyIterator& operator = (MyIterator& it)
            {
                Iter = it.Iter;
                return *this;
            }
            bool operator ==(const MyIterator& iter)
            {
                return Iter==iter.Iter?

true:false; } bool operator !=(const MyIterator& iter) { return Iter!=iter.Iter?true:false; } T operator *() { return Iter->data; } MyIterator &operator[](int n) { for(int i=0;i<n;i++) { assert(NULL!=Iter); Iter=Iter->next; } return *this; } MyIterator &operator +(int n) { for(int i=0;i<n;i++) { assert(NULL!=Iter); Iter=Iter->next; } return *this; } };// MyIterator 内部类 // public:// 功能函数 MyList<T> sublist(MyIterator&,MyIterator&); MyList<T> sublist(MyIterator&,int); MyList<T> sublist(MyIterator&); void push_back(const T); void push_front(const T); void pop_back(); void pop_front(); void clear(); void reserve(); void reserve(MyIterator&,MyIterator&); void replace(T,T); void swap(MyList<T>&); void display(); void display_rear(); void insert(MyIterator&,T); void f_insert(MyIterator&,T); void remove(T); bool exist(T); bool up_order(); bool down_order(); int get_size(){return size;} // swap 值 friend void swap(MyIterator& it1,MyIterator& it2) { (it1.Iter)->data ^= (it2.Iter)->data; (it2.Iter)->data ^= (it1.Iter)->data; (it1.Iter)->data ^= (it2.Iter)->data; } // erase 当前的it,返回it->next MyIterator erase(MyIterator& it) { assert(0 != size); size--; if(0 == size) { head = NULL; rear = NULL; return MyIterator(NULL); } if(it.Iter == head) { node *tp = head; head = head->next; head->last = NULL; delete(tp); return MyIterator(head); } if(it.Iter == rear) { node *tp = rear; rear = rear->last; rear->next = NULL; delete(tp); return MyIterator(NULL); } (it.Iter)->last->next = (it.Iter)->next; (it.Iter)->next->last = (it.Iter)->last; node *ans = it.Iter->next; delete(it.Iter); return MyIterator(ans);// 返回结果 } // find 返回ter指针 MyIterator find(T val) { node *tp = head; for(;tp!=NULL;tp=tp->next) { if((tp->data)==val) return MyIterator(tp); } return MyIterator(NULL); } MyIterator begin(){return MyIterator(head);} MyIterator end(){return MyIterator(rear);} T& front(){return head->data;} T& back(){return rear->data;} bool empty(){return 0==size?true:false;} void link(MyList<T>& list0){*this += list0;} void reset(MyIterator& it,T val){(it.Iter)->data = val;} // public://运算符重载 MyList<T>& operator = (const MyList<T>& list) { if(list.head == (*this).head) return *this; this->clear(); if(NULL==list.head) {//空的 this->head = NULL; this->rear = NULL; return *this; } if(1 == list.size) {//一个节点 //node *tp = new node; node *tp = new node; tp->data = list.head->data; head = tp; head->last = NULL; rear = tp; rear->next = NULL; size = 1; return *this; } node *cur = list.head; node *tp = new node; tp->data = cur->data; head = tp; head->last = NULL; rear = tp; while(cur->next!=NULL) { cur = cur->next; node *tp = new node; tp->data = cur->data; rear->next = tp; tp->last = rear; rear = tp; } rear->next = NULL; size = list.size; return *this; } // MyList 的+= 感觉能够改进的。别两个while了 MyList<T>& operator += (const MyList<T>& list) { if(list.head == (*this).head) {// meichuli //cout << "****" << endl; MyList<T> other(list); //cout << "&&&&&" << endl; return *this += other; } if(NULL == list.head)// list 空串 return *this; node *cur = list.head; node *tp = new node; tp->data = cur->data; if(head == NULL) {// 当前list为空的 head = tp; head ->last = NULL; rear = tp; } else { rear->next = tp; tp->last = rear; rear = tp; } while(cur->next!=NULL) { cur = cur->next; node *tp = new node; tp->data = cur->data; rear->next = tp; tp->last = rear; rear = tp; } rear->next = NULL; size += list.size; return *this; } // []得内容,与以下相应 T& operator [](int i) { assert(i<size); node* tp = head; while(i--) { tp = tp->next; } return tp->data; } // iterator 加i ,这个东西又问题。一直不知道怎么用的 MyIterator& operator +(int i) { assert(i<size); node* tp = head; while(i--) { tp = tp->next; } return MyIterator(tp); } };// MyList类 // constructor template<class T> MyList<T>::MyList() { head = NULL; rear = NULL; size = 0; } template<class T> MyList<T>::MyList(T val) { node *tp = new node; tp->data = val; size = 1; head = tp; rear = tp; rear->last = NULL; head->next = NULL; } // 对否 待定? template<class T> MyList<T>::MyList(T val,int n) { size = n; if(size!=0) { node *tp = new node; tp->data = val; head = tp; rear = tp; head->last = NULL; for(int i=1;i<n;i++) { node *tp = new node; tp->data = val; tp->last = rear; rear->next = tp; rear = tp; } rear->next = NULL; } } template<class T> MyList<T>::MyList(const MyList<T>& list0) { size = list0.size; node *cur0 = list0.head; node *tp = new node; tp->data = cur0->data; head = tp; rear = tp; head->last = NULL; while(cur0->next!=NULL) { cur0 = cur0->next; node *tp = new node; tp->data = cur0->data; tp->last = rear; rear->next = tp; rear = tp; } rear->next = NULL; } // clear template<class T> void MyList<T>::clear() { node *tp; for(;size!=0;size--) { tp = head; head = head->next; delete tp; } tp = NULL; } // 功能函数 // sublist // 这个有点问题的》》》》,从it1 到 it2;包含从头到尾,下标从0開始 template<class T> MyList<T> MyList<T>::sublist(MyIterator& it1,MyIterator& it2) { MyList<T>::MyIterator it0; int n = 1; for(it0=it1;it0!=it2&&n<1e9;++it0) { n++; } assert(n!=1e9); return this->sublist(it1,n); } template<class T> MyList<T> MyList<T>::sublist(MyIterator& it1,int n) { MyList<T> list0; list0.size = n; node *tp = new node; tp->data = *it1; list0.head = tp; list0.rear = tp; //rear->next = NULL; head->last = NULL; ++it1; for(int i=1;i<n;i++) { node *tp = new node; tp->data = *it1; list0.rear->next = tp; tp->last = list0.rear; list0.rear = tp; ++it1; } list0.rear->next = NULL; return list0; } template<class T> MyList<T> MyList<T>::sublist(MyIterator& it2) { return this->sublist(this->begin(),it2); } // display 从头開始 template<class T> void MyList<T>::display() { if(NULL==this->head) { cout << ‘\0‘ << endl; return; } MyList<T>::MyIterator iter = this->begin(); for(;iter!=this->end();++iter) { cout << *(iter) << " "; } cout << *(iter) << endl;// 输出end end是rear } // display_rear template<class T> void MyList<T>::display_rear() { if(NULL==this->rear) { cout << ‘\0‘ << endl; return; } MyList<T>::MyIterator iter = this->end(); for(;iter!=this->begin();--iter) { cout << *(iter) << " "; } cout << *(iter) << endl;// begin() } // push_back template<class T> void MyList<T>::push_back(const T val) { node *tp = new node; tp->data = val; size++; if(1==size) { head = tp; rear = tp; head->last = NULL; rear->next = NULL; } else { rear->next = tp; tp->last = rear; rear = tp; rear->next = NULL; } } // push_front template<class T> void MyList<T>::push_front(const T val) { node *tp = new node; tp->data = val; size++; if(1==size) { head = tp; rear = tp; head->last = NULL; rear->next = NULL; } else { tp->next = head; head->last = tp; head = tp; head->last = NULL; } } // pop_back template<class T> void MyList<T>::pop_back() { assert(rear!=NULL); size--; node *tp = rear; rear = rear->last; rear->next = NULL; delete tp; tp = NULL; } // pop_front template<class T> void MyList<T>::pop_front() { assert(head!=NULL); size--; node *tp = head; head = head->next; head->last = NULL; delete tp; tp = NULL; } // reverse template<class T> void MyList<T>::reserve() { node *le = head; node *ri = rear; for(int c=size;c>1;c-=2) { // 值交换,指针交换?? le->data ^= ri->data; ri->data ^= le->data; le->data ^= ri->data; le = le->next; ri = ri->last; } } // reverse 对于同一个串进行交换。

。 template<class T> void MyList<T>::reserve(MyIterator& it1,MyIterator& it2) { node *le = it1.Iter; node *ri = it1.Iter; int c = 1; // 不是非常理解的 for(;ri!=it2.Iter;ri=ri->next) { c++; } for(;c>1;c-=2) { // 值交换,指针交换?? le->data ^= ri->data; ri->data ^= le->data; le->data ^= ri->data; le = le->next; ri = ri->last; } } // replace template<class T> void MyList<T>::replace(T val1,T val2) { node *tp = head; while(tp!=NULL) { if(tp->data==val1) { tp->data = val2; } tp = tp->next; } } // swap list //template<class T> //void MyList<T>::swap_error(MyList<T>& list0) //{ // node *tp = head; // head = list0.head; // list0.head = tp; // tp = rear; // rear = list0.rear; // list0.rear = rear; // size ^= list0.size; // list0.size ^= size; // size ^= list0.size; //} template<class T> void MyList<T>::swap(MyList<T>& list0) { // 交换head node *tp = head->next; head->next = (list0.head)->next; (list0.head)->next->last = head; (list0.head)->next = tp; tp->last = list0.head; // 交换size size ^= list0.size; list0.size ^= size; size ^= list0.size; // 交换head->data head->data ^= (list0.head)->data; (list0.head)->data ^= head->data; head->data ^= (list0.head)->data; // 交换rear tp = rear->last; rear->last = (list0.rear)->last; (list0.rear)->last->next = rear; (list0.rear)->last = tp; tp->next = list0.rear; //交换rear->data rear->data ^= (list0.rear)->data; (list0.rear)->data ^= rear->data; rear->data ^= (list0.rear)->data; } // insert template<class T> void MyList<T>::insert(MyIterator& it,T val) { node *tp = new node; tp->data = val; if(NULL==it.Iter->next) {// 尾的后面 rear->next = tp; tp->last = rear; rear = tp; size++; } else { tp->next = (it.Iter)->next; (it.Iter)->next->last = tp; (it.Iter)->next = tp; tp->last = (it.Iter); size++; } } template<class T> void MyList<T>::f_insert(MyIterator& it,T val) { node *tp = new node; tp->data = val; if(NULL==it.Iter->last) {// head head->last = tp; tp->next = head; head = tp; size++; } else { tp->last = (it.Iter)->last; (it.Iter)->last->next = tp; tp->next = it.Iter; (it.Iter)->last = tp; size++; } } // remove value template<class T> void MyList<T>::remove(T val) { node *tp = head; for(;tp!=NULL;) { if(tp->data==val) { tp = this->erase(MyIterator(tp)).Iter; } else { tp = tp->next; } } } // exist template<class T> bool MyList<T>::exist(T val) { node *tp = head; for(;tp!=NULL;tp=tp->next) { if(tp->data==val) { return true; } } return false; } // is up_order template<class T> bool MyList<T>::up_order() { node *tp = head; if(NULL==head || NULL==tp->next) return true; for(;tp->next!=NULL;tp=tp->next) { if(tp->data>tp->next->data) { return false; } } return true; } // is down_order template<class T> bool MyList<T>::down_order() { node *tp = head; if(NULL==head || NULL==tp->next) return true; for(;tp->next!=NULL;tp=tp->next) { if(tp->data<tp->next->data) { return false; } } return true; } #endif // MYLIST_H_INCLUDED


(5) 測试韩式main

#include "MyList.h"

using namespace std;
int main()
{
    MyList<string> list0;
    MyList<string> list7("ccc",3);
    list0 += list7;
    list0.display_rear();
    string tmp = "aaa";
    list0.push_back(tmp);
    list0.push_front("bbbb");

    list7.display();
    list0 += list0;
    list0.display_rear();
    cout << "1: " << list0.get_size() << " ," <<  endl;
    //MyList<string> list1(tmp);
    list0.clear();
    //list1.display();
    //list1.clear();
    //list1.display();
    MyList<int> list1(5,4);
    list1.push_back(1111);
    MyList<int>::MyIterator it = list1.begin();
    //++it;
    list1.insert(it+2,2222);
    list1.f_insert(it,555);
    //list1.push_front(5555);
    //list1.pop_back();
    //list1.pop_front();
    cout << "2: " << list1.get_size() << ", ";
    list1.display();
    MyList<int> list2(list1);
    cout << "3: " << list2.get_size() << ", ";
    list2.display();
    list2.reserve(list2.begin()+2,list2.begin()+4);
    list2.display_rear();
    MyList<int> list3(3,6);
    list3.swap(list1);
    list1.reserve();
    cout << "4: " << list1.get_size() << ",";
    list1.display();
    list1.push_back(33);
    cout << "5:" << list1[6] << ",," << list1.down_order() << list1.up_order() << endl;
    //list1.display_rear();
    MyList<int> list4= list2.sublist(list2.begin()+2,list2.begin()+4);
    list4.display();
    return 0;
}




以上是关于STL 之 list源码自行实现(iterator)的主要内容,如果未能解决你的问题,请参考以下文章

STL源码剖析之Iterator

STL源码剖析之Iterator

stl源码剖析-序列式容器 之 list

《STL 源码剖析》学习笔记之容器list

STL源码分析归档

STL之iterator