list的结构与vector、deque完全不同,list对象提供了头尾指针,分别指向前后元素。因此list与vector和deuqe不同,比如:
1.list不支持随机访问,访问元素的效率较低;
2.执行元素的插入和移除都很快,始终是常量时间内完成,因为无需移动任何元素;
3.安插和删除的操作不会因此指向其他元素的指针、引用、迭代器失效;
4.list对异常的控制要么成功,要么什么都不发生。
下面上具体代码:
template< typename D>
class CStlExList
{
public:
CStlExList() {
}
virtual ~CStlExList() { this->XTTClear(); }
public:
D* XTTAddItem( bool isback=true )
{
D *pItem = new D();
if(!pItem)
return 0;
if( isback )
m_XTTList.push_back( pItem );
else
m_XTTList.push_front( pItem );
return pItem;
}
D *XTTPopFront()
{
if( m_XTTList.empty() )
return 0;
D*pItem = m_XTTList.front();
m_XTTList.pop_front();
return pItem;
}
void XTTClear()
{
typename StlContainerList::iterator it = m_XTTList.begin();
for(;it != m_XTTList.end(); it++)
{
D *pItem = *it;
delete pItem;
}
m_XTTList.clear();
}
void XTTGetAllItem(list<D*> &tList)
{
typename StlContainerList::iterator it = m_XTTList.begin();
for(;it != m_XTTList.end(); it++) {
tList.push_back( *it );
}
}
int XTTGetTotalCt(){ return m_XTTList.size(); }
protected:
typedef list<D*> StlContainerList;
StlContainerList m_XTTList;
};