#include
using namespace std;
templateclass list {
public:
// 缺省构造
list() : m_head(NULL), m_tail(NULL) {}
// 拷贝构造
list( const list& that ) : m_head(NULL),m_tail(NULL) {
for( node* pnode=that.m_head; pnode!=NULL; pnode=pnode->m_next ) {
push_back(pnode->m_data);
}
}
// 析构函数
~list() {
clear();
}
// 链表判空
bool empty() const {
return m_headNULL && m_tailNULL;
}
// 添加头节点
void push_front( const T& data ) {
m_head = new node( NULL, data, m_head );
if( m_head->m_next )
m_head->m_next->m_prev = m_head;
else
m_tail = m_head;
}
// 添加尾节点
void push_back( const T& data ) {
m_tail = new node( m_tail, data, NULL );
if( m_tail->m_prev )
m_tail->m_prev->m_next = m_tail;
else
m_head = m_tail;
}
// 删除头节点
void pop_front() throw(underflow_error) {
if( empty() )
throw underflow_error(“null node”);
node* pnext = m_head->m_next;
delete m_head;
if( pnext )
pnext->m_prev = NULL;
else
m_tail = NULL;
m_head = pnext;
}
// 删除尾节点
void pop_back() {
if( empty() )
throw underflow_error(“null node”);
node* prev = m_tail->m_prev;
delete m_tail;
if( prev )
prev->m_next = NULL;
else
m_head = NULL;
m_tail = prev;
}
// 获取头节点数据
T front() const throw(out_of_range) {
if( empty() )
throw out_of_range(“null node”);
return m_head->m_data;
}
// 获取尾节点数据
T back() const throw(out_of_range) {
if( empty() )
throw out_of_range(“null node”);
return m_tail->m_data;
}
// 链表清空
void clear() {
while( !empty() ) {
pop_front();
}
}
// 获取链表大小
size_t size() const {
size_t i = 0;
for( node* pnode=m_head; pnode!=NULL; pnode=pnode->m_next ) {
i;
}
return i;
}
private:
// 节点类
class node {
public:
node( node* prev, const T& data, node* next ) : m_prev(prev),m_data(data),m_next(next) {}
node* m_prev; // 前指针
T m_data;
node* m_next; // 后指针
};
public:
// 非常迭代类(用于非常容器)
class iterator {
public:
iterator( node* start, node* cur, node* end ) : m_start(start),m_cur(cur),m_end(end) {}
iterator& operator() {
if( m_cur == NULL )
m_cur = m_start;
else
m_cur = m_cur->m_next;
return this;
}
iterator& operator–() {
if( m_curNULL )
m_cur = m_end;
else
m_cur = m_cur->m_prev;
return this;
}
T& operator() throw(out_of_range) {
if( m_curNULL )
throw out_of_range(“null node”);
return m_cur->m_data;
}
bool operator==( const iterator& that ) const {
return m_startthat.m_start && m_curthat.m_cur && m_endthat.m_end;
}
bool operator!=( const iterator& that ) const {
return !(*thisthat);
}
private:
node m_start; // 开始指向
node* m_cur; // 当前指向
node* m_end; // 终止指向
friend class list;
};
// 常迭代类(用于常容器)
class const_iterator {
public:
const_iterator( node* start, node* cur, node* end ) : m_start(start),m_cur(cur),m_end(end) {}
const_iterator& operator++() {
if( m_cur == NULL )
m_cur = m_start;
else
m_cur = m_cur->m_next;
return this;
}
const_iterator& operator–() {
if( m_cur == NULL )
m_cur = m_end;
else
m_cur = m_cur->m_prev;
return this;
}
const T& operator() throw(out_of_range) { // 重点体会为什么在这里加const
if( m_curNULL )
throw out_of_range(“null node”);
return m_cur->m_data;
}
bool operator( const const_iterator& that ) const {
return m_startthat.m_start && m_curthat.m_cur && m_endthat.m_end;
}
bool operator!=( const const_iterator& that ) const {
return !(*thisthat);
}
private:
node m_start;
node* m_cur;
node* m_end;
};
// 创建非常起始迭代器(用于遍历)
iterator begin() {
return iterator(m_head,m_head,m_tail);
}
// 创建非常终止迭代器(结束标识)
iterator end() {
return iterator(m_head,NULL,m_tail);
}
// 创建常起始迭代器(用于遍历)
const_iterator begin() const {
return const_iterator(m_head,m_head,m_tail);
}
// 创建常终止迭代器(结束标识)
const_iterator end() const {
return const_iterator(m_head,NULL,m_tail);
}
// 在迭代器指向的位置添加节点
void insert( const iterator& loc, const T& data ) {
if( loc == end() ) {
push_back( data );
} else {
node* pnew = new node(loc.m_cur->m_prev, data, loc.m_cur );
if( pnew->m_prev )
pnew->m_prev->m_next = pnew;
else
m_head = pnew;
pnew->m_next->m_prev = pnew;
}
}
// 删除迭代器指向的节点
void erase( const iterator& loc ) {
if( locend() )
throw out_of_range(“null node”);
node* pdel = loc.m_cur;
if( pdel->m_prev )
pdel->m_prev->m_next = pdel->m_next;
else {
pdel->m_next->m_prev = NULL;
m_head = pdel->m_next;
}
if( pdel->m_next )
pdel->m_next->m_prev = pdel->m_prev;
else {
pdel->m_prev->m_next = NULL;
m_tail = pdel->m_prev;
}
delete pdel;
}
private:
node* m_head; // 链表头指针
node* m_tail; // 链表尾指针
};
// 利用"“比较查找
template<typename IT, typename T> IT find( const IT& beg, const IT& end, const T& data ) {
for( IT it=beg; it!=end; ++it ) {
if( *it==data ) {
return it;
}
}
return end;
}
// 利用”<"实现的排序
templatevoid sort( const IT& beg, const IT& end ) {
IT last = end;
–last;
IT p = beg;
for( IT i=beg, j=last; i!=j; ) {
while( i!=p && *i<*p ) {
++i;
}
if( i!=p ) {
swap( *i, *p );
}
while( j!=p && *p<*j ) {
–j;
}
if( j!=p ) {
swap( *p, *j );
}
}
IT it = beg;
++it;
if( p!=beg && p!=it ) {
sort( beg, p );
}
it = p;
++it;
if( it!=end && it!=last ) {
sort( it, end );
}
}
// 容器设计者提供比较类
templateclass Greater {
public:
bool operator()( T x, T y) {
return x > y;
}
};
// 利用"比较器"实现的排序
template<typename IT, typename CMP>void sort( const IT& beg, const IT& end, CMP cmp ) {
IT last = end;
–last;
IT p = beg;
for( IT i=beg, j=last; i!=j; ) {
while( i!=p && cmp(*i,*p) ) { // cmp.operator()(*i,*p)
++i;
}
if( i!=p ) {
swap( *i, *p );
}
while( j!=p && cmp(*p,*j) ) { // cmp.operator()(*p,*j)
–j;
}
if( j!=p ) {
swap( *p, *j );
}
}
IT it = beg;
++it;
if( p!=beg && p!=it ) {
sort( beg, p, cmp );
}
it = p;
++it;
if( it!=end && it!=last ) {
sort( it, end, cmp );
}
}
// 以上代码容器的设计者
// -----------------------
// 以下代码容器的使用者
void Print( const string& str, const list& l ) {
cout << str << endl;
typedef list::const_iterator CIT;
for( CIT it=l.begin(); it!=l.end(); ++it ) {
cout << *it << ’ ';
}
cout << endl << “----------------------------” << endl;
}
// 用户设计比较类
class ZJW {
public:
bool operator()( int x, int y ) {
return x < y;
}
};
int main( void ) {
list ls; // 非常容器(非常对象)
for( int i=0; i<5; i++ ) { ls.push_front(1000+i); } for( int i=0; i<5; i++ ) { ls.push_back(100+i); } Print( "添加头尾节点后:", ls ); ls.pop_front(); ls.pop_back(); Print( "删除头尾节点后:", ls ); ls.insert( ++ls.begin(), 888 ); // 增 Print( "迭代器指向的位置添加节点后:", ls ); ls.erase( ----ls.end() ); // 删 Print( "删除迭代器指向的节点后:", ls ); typedef list<int>::iterator IT; IT it = ls.begin(); *it = 999; // 改 Print( "修改迭代器指向的节点后:", ls ); IT fit = find( ls.begin(), ls.end(), 100 ); // 查 if( fit != ls.end() ) { ls.erase( fit ); } Print( "找到100并删除后:", ls );
// sort( ls.begin(), ls.end() ); // 利用<排序
// sort( ls.begin(), ls.end(), ZJW() ); // 利用 用户自己设计的比较器
Greater<int> g;// 容器设计者提供的比较器 sort( ls.begin(), ls.end(), g ); // 利用 容器设计者提供的比较器 Print( "排序后:", ls ); const list<int> cls = ls; // 常容器(常对象) Print( "常容器:", cls ); sort( cls.begin(), cls.end(), ZJW() ); Print( "排序后:", cls ); return 0;
}
帮我注释这段代码,并且为我解释里面迭代器有关的知识