當C++遇到iOS應用開發之—List集合

來源:互聯網
上載者:User
      在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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.