手机版
你好,游客 登录 注册
背景:
阅读新闻

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

[日期:2012-11-07] 来源:www.cnblogs.com/daizhj/  作者:daizhj, 代震军 [字体:大 中 小]

在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. }; 
linux
相关资讯       C++  iOS应用开发 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款