当C++遇到iOS应用开发之---List集合


在Object-C中,数组使用NSArray和NSMutableArray(可变长数组)。使用语法如下:

NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil]; 

取数组元素的方法:

[array objectAtIndex:2]); 

因为数组在开发中会被频繁使用,且objectAtIndex的写法看着过于繁复,远不如array[2]这种直观。所以我将C++中的vector类进行了封装,并增加一些新的功能:

  1. #include <iostream>  
  2. #include <vector>  
  3.  
  4. using namespace std; 
  5. using namespace std::tr1; 
  6. template<typename T, typename _Alloc = std::allocator<T> > 
  7. class  List: public vector<T, _Alloc> 
  8. private
  9.     typedef typename std::vector<T>::iterator list_it; 
  10.      
  11.     list_it _it; 
  12.  
  13. public
  14.     List(){} 
  15.      
  16.     List(NSArray *array){ 
  17.         copyFromArray(array); 
  18.     } 
  19.      
  20.        
  21.     List(string *array){ 
  22.         copyFromArray(array); 
  23.     } 
  24.      
  25.     List(int *array){ 
  26.         copyFromArray(array); 
  27.     } 
  28.      
  29.     ~List() 
  30.     { 
  31.         std::cout<<"list destroy!"<<endl; 
  32.     } 
  33.      
  34.     List& add(const T t){ 
  35.         this->push_back(t); 
  36.         return (*this); 
  37.     } 
  38.      
  39.     void clear(){ 
  40.         //this->clear();  
  41.         this->erase(this->begin(), this->end()); 
  42.     } 
  43.      
  44.  
  45.     BOOL contains(const T t){ 
  46.         return indexOf(t) >= 0; 
  47.     } 
  48.      
  49.  
  50.     int indexOf(const T t){ 
  51.         list_it it = std::find(this->begin(),this->end() , t ) ; 
  52.         if(it != this->end()) 
  53.             return it - this->begin(); 
  54.         return -1 ; 
  55.     } 
  56.  
  57.     void insert(int index,  const T t) 
  58.     { 
  59.         this->insert(this.begin() + index, 1, t); 
  60.     } 
  61.          
  62.  
  63.     void remove(const T t) 
  64.     { 
  65.         int pos = indexOf(t); 
  66.         if (pos >= 0) 
  67.             this->removeAt(pos); 
  68.     } 
  69.  
  70.     void removeAt(int index) 
  71.     { 
  72.         if (this->size() > index){ 
  73.             this->erase(this->begin() + index, this->begin() + index + 1); 
  74.         } 
  75.     } 
  76.  
  77.     int count() 
  78.     { 
  79.         return this->size(); 
  80.     } 
  81.  
  82.     void copyFrom(List<T> list) 
  83.     { 
  84.          this->assign(list.begin(), list.end()); 
  85.     } 
  86.      
  87.     void copyFromArray(NSArray *array){ 
  88.          for(int i = 0; i< [array count]; i++){ 
  89.              T t = (T)[array objectAtIndex:i]; 
  90.              this->push_back(t); 
  91.          } 
  92.     } 
  93.      
  94.     void copyFromArray(string* array){ 
  95.         for(int i = 0; i< array->length(); i++){ 
  96.             T t = (T)array[i]; 
  97.             this->push_back(t); 
  98.         } 
  99.     } 
  100.      
  101.     void copyFromArray(int* array){ 
  102.         for(int i = 0; i<(sizeof(array)/sizeof(int)); i++){ 
  103.             T t = (T)array[i]; 
  104.             this->push_back(t); 
  105.             //this->vector<T>::push_back(new T);//usage:用父类方法 或 static_cast<vector<T>*>(this)->push_back(T);  
  106.         } 
  107.     } 
  108. }; 
  • 1
  • 2
  • 下一页

相关内容