Recently, C ++ has been used to study std for several days. It is found that std: list provides similar functions as ArrayList in Java, but it is very different in programming usage and is not used to it. Therefore, a std: list encapsulation class is written. ArrayList. h [cpp]/********************************* *************************************//* arrayList * // * a Java-like encapsulation of std:: list encapsulation class (the header file contains the function implementation body) * // * author: Sinmax, date: *//************************************ * **********************************/# ifndef _ ARRAY_LIST_H _ # define _ ARRAY_LIST_H _ # include <list> using namespace std; template <class T> class ArrayList {public: typedef ty Pename std: list <T >:: iterator ArrayListIterator; ArrayList (): m_list (0) {m_list = new std: list <T> ();};~ ArrayList () {m_list-> clear (); delete m_list;}; // add an element (to the end) void add (T t) {m_list-> push_back (t);} // add an element: if the specified index is greater than the list length, add it to the end 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) ;}// gets the element T get (int index) of the specified 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;} // Delete the specified Element (first found) void remove (T t) {ArrayLi StIterator it = indexOfIt (t); if (it! = M_list-> end () {m_list-> erase (it) ;}/// Delete the specified Element (all found) void removeEx (T t) {m_list-> remove (t);} // Delete the element of the specified index and return the element T removeAt (int index) {if (index <0) of the index) {// 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_lis T-> 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;} // delete all elements void removeAll () {m_list-> clear () ;}// find the 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 ();} // search for the index 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 ;} // search for the index 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;} // export the list as an array 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 ;} // import n elements from the array void fromArray (T * array, int n) {for (int I = 0; I <n; I ++) {add (array [I]) ;}} void sort (/* _ Pr3 _ Pred */) {// not implemented} // return the total number of container elements int count () {if (m_list-> empty () return 0; return m_list-> size ();} // copy a copy of the current instance 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;} // obtain the m_list instance. The external program directly operates std: list <T> * getStdList () {return m_list;} private: // unsigned int overflow std: list <T> * m_list;}; # endif Test01.cpp (compiled by VS2010) [cpp] # 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,100 00 ); www.2cto.com 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 ;}