当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类进行了封装,并增加一些新的功能:

#include <iostream>
#include <vector>

using  namespace std;
using  namespace std::tr1;
template<typename T, typename _Alloc = std::allocator<T> >
class  List:  public vector<T, _Alloc>
{
private:
    typedef typename std::vector<T>::iterator list_it;
   
    list_it _it;

public:
    List(){}
   
    List(NSArray *array){
        copyFromArray(array);
    }
   
      
    List( string *array){
        copyFromArray(array);
    }
   
    List( int *array){
        copyFromArray(array);
    }
   
    ~List()
    {
        std::cout<< " list destroy! "<<endl;
    }
   
    List& add( const T t){
         this->push_back(t);
         return (* this);
    }
   
     void clear(){
         // this->clear();
         this->erase( this->begin(),  this->end());
    }
   

    BOOL contains( const T t){
         return indexOf(t) >=  0;
    }
   

     int indexOf( const T t){
        list_it it = std::find( this->begin(), this->end() , t ) ;
         if(it !=  this->end())
             return it -  this->begin();
         return - 1 ;
    }

     void insert( int index,   const T t)
    {
         this->insert( this.begin() + index,  1, t);
    }
       

     void remove( const T t)
    {
         int pos = indexOf(t);
         if (pos >=  0)
             this->removeAt(pos);
    }

     void removeAt( int index)
    {
         if ( this->size() > index){
             this->erase( this->begin() + index,  this->begin() + index +  1);
        }
    }

     int count()
    {
         return  this->size();
    }

     void copyFrom(List<T> list)
    {
          this->assign(list.begin(), list.end());
    }
   
     void copyFromArray(NSArray *array){
          for( int i =  0; i< [array count]; i++){
             T t = (T)[array objectAtIndex:i];
              this->push_back(t);
         }
    }
   
     void copyFromArray( string* array){
         for( int i =  0; i< array->length(); i++){
            T t = (T)array[i];
             this->push_back(t);
        }
    }
   
     void copyFromArray( int* array){
         for( int i =  0; i<( sizeof(array)/ sizeof( int)); i++){
            T t = (T)array[i];
             this->push_back(t);
             // this->vector<T>::push_back(new T); // usage:用父类方法 或 static_cast<vector<T>*>(this)->push_back(T);
        }
    }
};



用法如下

    实例化对象并添加数据:
    List<NSString*> list;
    list.add( @" 1 ");
    list.add( @" 2代震军 ");
    list.add( @" 333 ").add( @" 44444 ").add( @" 5 ");

    或用下面方式:
    NSArray *array = [[NSArray alloc] initWithObjects:
                       @" One ", @" Two ", @" Three ", @" Four ",nil];
    List<NSString*> list1(array);

    判断是否存在某数据:
    NSString *del =  @" 44444 ";
     bool iscontains = list.contains(del);

     删除数据:
    list.removeAt( 2);
    list.remove(del);

    遍历:
     for(List<NSString*>::iterator it = list.begin() ;it != list.end() ; it++)
        cout << [(*it) UTF8String ]<< "   " ;

    或使用foreach:
    __block NSString* str;
    for_each(list.begin(), list.end(), ^(NSString *value){
        str = value;
        std::cout<<[value UTF8String]<<endl;
    });


    获取指定索引记录:
    NSString * result = list[ 0];


    代码比较简单,呵呵

    好了,今天的内容就先到这里了。

    原文链接:http://www.cnblogs.com/daizhj/archive/2012/11/07/2758843.html

    作者: daizhj, 代震军 
    微博: http://weibo.com/daizhj
    Tags:ios, c++, NSArray, NSMutableArray, vector
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值