Recently, the project needs to use C++, and I have studied std for a few days. I found that the function provided by std::list is similar to that of Java's ArrayList, but there are big differences in programming use, and I feel very uncomfortable, so I wrote a std::list package class.
ArrayList.h
/************************************************************************/
/* ArrayList */
/* 一个仿Java类封装的std::list封装类(请自行分拆头文件和函数实现体) */
/* author:Sinmax, date:2013-02-25 */
/************************************************************************/
#ifndef _ARRAY_LIST_H_
#define _ARRAY_LIST_H_
#include <list>
using namespace std;
template <class T>
class ArrayList
{
public:
typedef typename std::list<T>::iterator ArrayListIterator;
ArrayList()
: m_list(0)
{
m_list = new std::list<T>();
};
~ArrayList()
{
m_list->clear();
delete m_list;
};
//添加一个元素(到末端)
void add(T t)
{
m_list->push_back(t);
}
//添加一个元素:在指定索引,指定索引大于列表长度,则添加到末端
void add(T t, int index )
{
if( index < 0 )
{
//ignore , not recommand m_list->push_front(t);
}
else if( index == 0 )
{
m_list->push_front(t);
}
else if( index < count() - 1 )
{
ArrayListIterator it = m_list->begin();
int n = 0;
while( n < index )
{
++it;
++n;
}
m_list->insert(it, t);
}
else
{
m_list->push_back(t);
}
}
//获取指定索引的元素
T get(int index)
{
int length = count();
if( index == 0 )
{
return m_list->front();
}
else if( index < length - 1 )
{
int n = 0;
ArrayListIterator it = m_list->begin();
while ( ++n <= index )
{
++it;
}
return *it;
}
else if( index == length - 1 )
{
return m_list->back();
}
return NULL;
}
//删除指定的元素(第一个找到)
void remove(T t)
{
ArrayListIterator it = indexOfIt(t);
if( it != m_list->end() )
{
m_list->erase(it);
}
}
//删除指定的元素(所有找到)
void removeEx(T t)
{
m_list->remove(t);
}
//删除指定索引的元素,并返回该索引的元素
T removeAt(int index)
{
if( index < 0 )
{
//ignore
return NULL;
}
int length = count();
T t = NULL;
if ( index >= length )
{
return NULL;
}
else if ( index == 0 )
{
t = m_list->front();
m_list->pop_front();
return t;
}
else if( index == length - 1 )
{
t = m_list->front();
m_list->pop_front();
return t;
}
else if( index < length )
{
ArrayListIterator it = m_list->begin();
while( --length > 0 )
{
++it;
}
t = *it;
m_list->erase(it);
return t;
}
return NULL;
}
//删除所有元素
void removeAll()
{
m_list->clear();
}
//查找等于t元素的迭代器
const ArrayListIterator indexOfIt(T t)
{
ArrayListIterator it = m_list->begin();
while( it != m_list->end() )
{
if( t == *it )
return it;
else
++it;
}
return m_list->end();
}
//查找t在列表中的索引
int indexOf(T t)
{
ArrayListIterator it = m_list->begin();
int index = 0;
while( it != m_list->end() )
{
if( t == *it )
return index;
++it ;
++index;
}
return -1;
}
//从末端起 查找t在列表中的索引
int lastIndexOf(T t)
{
ArrayListIterator it = m_list->end();
int index = count();
do
{
--it;
--index;
if( t == *it )
return index;
} while ( it != m_list->begin() ) ;
return -1;
}
//把列表导出为数组
T* toArray(T* array = NULL)
{
int cnt = count();
if( cnt == 0 )
return NULL;
if( array == NULL)
array = new T[cnt];
int index = 0;
ArrayListIterator it = m_list->begin();
while ( it != m_list->end() )
{
array[index] = *it;
++index;
++it;
}
return array;
}
//从数组里导到n个元素
void fromArray(T* array, int n)
{
for(int i=0;i<n;i++)
{
add(array[i]);
}
}
void sort( /* _Pr3 _Pred */ )
{
//未实现
}
//返回容器元素总个数
int count()
{
if( m_list->empty() )
return 0;
return m_list->size();
}
//复制一份当前实例的副本
ArrayList* clone()
{
//m_list->insert( .... )
ArrayList<T>* array = new ArrayList<T>();
T* elements = this->toArray();
int cnt = this->count();
array->fromArray(elements, cnt);
delete elements;
return array;
}
//获取m_list实例,由外部程序直接操作
std::list<T>* getStdList()
{
return m_list;
}
private:
//unsigned int overflow
std::list<T>* m_list;
};
#endif
Test01.cpp (compiled under VS2010)
#include "stdafx.h"
#include "ArrayList.h"
int _tmain(int argc, _TCHAR* argv[])
{
ArrayList<int>* array = new ArrayList<int>();
for(int i=0;i<10;i++)
{
array->add( i );
}
array->add(2, 2);
array->addAt(100, 10000);
printf("indexOf(2)=%d \n\r", array->indexOf(2));
printf("lastIndexOf(2)=%d \n\r", array->lastIndexOf(2));
//array->removeEx(2);
//array->removeAll();
ArrayList<int>* array2 = array->clone();
for( int i=0;i<array->count();i++)
{
printf("get(%d)=%d \n\r", i, array->get(i));
}
getchar();
return 0;
}