做那个网站大全,湘潭网站建设 搜搜磐石网络,那些网站可以给产品做推广,注册公司那家网站做的比较好这是关于一个普通双非本科大一学生的C的学习记录贴
在此前#xff0c;我学了一点点C语言还有简单的数据结构#xff0c;如果有小伙伴想和我一起学习的#xff0c;可以私信我交流分享学习资料
那么开启正题
今天分享的是关于list的模拟实现#xff0c;今天只实现一部分基…这是关于一个普通双非本科大一学生的C的学习记录贴
在此前我学了一点点C语言还有简单的数据结构如果有小伙伴想和我一起学习的可以私信我交流分享学习资料
那么开启正题
今天分享的是关于list的模拟实现今天只实现一部分基础函数重点是const迭代器
1.iterator的访问
由于访问方式的不同iterator的类实现*的重载同时还应该实现-的重载
T operator*()
{return _node-_data;
}T* operator-()
{return _node-_data;
} 2.自定义类型相关
当list中存储的是自定义类型时我们来看下面的代码
struct Date
{int _year;int _month;int _day;Date(int year 0,int month 1,int day 1):_year(year),_month(month),_day(day){}
};void Test_list2()
{listDate l;l.push_back(Date(2006,9,12));l.push_back(Date(1999,5,6));listDate::iterator it l.begin();while(it ! l.end()){cout it-_year - it-_month - it-_day endl;it;}
}
很明显如果要通过it访问_year或_month_day应该是it--_year
第一次-通过it访问到Date第二次-通过Date再访问到_year
而我们要写出上面说的代码是不能编译通过的vs环境下而写一次-可以编译通过这是因为为了增强代码的可读性编译器在这里特殊处理了不需要我们写两次一次即可
3.const迭代器
实际运用中我们难免遇到const迭代器的存在
const迭代器是const对象的迭代器实际运用中我们不会自己创建const对象因为没有实际意义而在一些函数调用中难免出现const修饰的情况这个时候const迭代器的存在就不可或缺了
对于const与非const的存在我们需要对模板进行一些改造
typedef __list_iteratorT, T, T* iterator;
typedef __list_iteratorT, const T, const T* const_iterator; 对应的有下面这些迭代器相关函数
iterator begin()
{return iterator(_head-_next);
}iterator end()
{return iterator(_head);
}const_iterator begin() const
{return const_iterator(_head-next);
}const_iterator end() const
{return const_iterator(_head);
}
下面是完整的代码以及测试代码
#includeiostream
#includelistusing namespace std;namespace wkl
{templateclass Tstruct __list_node{__list_node* _next;__list_node* _prev;T _data;__list_node(const T x T()):_next(nullptr),_prev(nullptr),_data(x){}};// T T T*templateclass T, class Ref, class Ptrstruct __list_iterator{typedef __list_nodeT Node;typedef __list_iteratorT, Ref, Ptr Self;Node* _node;__list_iterator(Node* node nullptr):_node(node){}Ref operator*(){return _node-_data;}Ptr operator-(){return _node-_data;}Self operator(){_node _node-_next;return *this;}Self operator(int){Self tmp(*this);_node _node-_next;return tmp;}Self operator--(){_node _node-_prev;return *this;}Self operator--(int){Self tmp(*this);_node _node-_prev;return tmp;}bool operator!(const Self it) const {return _node ! it._node;}bool operator(const Self it) const {return _node it._node;}templateclass Tclass list{typedef __list_nodeT Node;public:typedef __list_iteratorT, T, T* iterator;typedef __list_iteratorT, const T, const T* const_iterator;list(){_head new Node;_head-_next nullptr;_head-_prev nullptr;}~list(){clear();delete[] _head;_head nullptr;}void clear(){iterator it begin();while (it ! end()){erase(it);}}iterator begin(){return iterator(_head-_next);}iterator end(){return iterator(_head);}const_iterator begin() const{return const_iterator(_head-next);}const_iterator end() const{return const_iterator(_head);}void push_back(const T x){Node* tail _head-_prev;Node* newnode new Node(x);newnode-_next _head;newnode-_prev tail;tail-_next newnode;_head-_prev newnode;}private:Node* _head;};};void Test_list1(){listint l;l.push_back(1);l.push_back(2);l.push_back(3);l.push_back(4);listint::iterator it l.begin();while (it ! l.end()){cout *it ;it;}cout endl;}struct Date{int _year;int _month;int _day;Date(int year 0,int month 1,int day 1):_year(year),_month(month),_day(day){}};void Test_list2(){listDate l;l.push_back(Date(2006,9,12));l.push_back(Date(1999,5,6));listDate::iterator it l.begin();while(it ! l.end()){cout it-_year - it-_month - it-_day endl;it;}} void list_print(const listint l){listint::const_iterator it l.begin();while (it ! l.end()){//*it 1;cout *it ;it;}cout endl;}void Test_list3(){listint l;l.push_back(1);l.push_back(2);l.push_back(3);l.push_back(4);l.push_back(5);list_print(l);}
}int main()
{wkl::Test_list3();return 0;
}新手写博客有不对的位置希望大佬们能够指出也谢谢大家能看到这里让我们一起学习进步吧