歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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