When C ++ encounters iOS app development-List Set

Source: Internet
Author: User
In Object-c, the array uses NSArray and NSMutableArray (Variable Length array ). The syntax is as follows:
NSArray * array = [[NSArray alloc] initWithObjects: @ "One", @ "Two", @ "Three", @ "Four", nil];

Method for retrieving array elements: [array objectAtIndex: 2]);

Because arrays are frequently used during development, and the objectAtIndex writing method is too complex, it is far less intuitive than array [2. So I encapsulated the vector class in C ++ and added some new features:

# 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 ){
This-> push_back (t );
Return (* this );
}

Void clear (){
// This-> clear ();
This-> erase (this-> begin (), this-> end ());
}

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

Int indexOf (const 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)
{
This-> insert (this. begin () + index, 1, t );
}

Void remove (const 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: Use the parent class method or static_cast <vector <T> *> (this) -> push_back (T );
}
}
};

Usage:

Instantiate the object and add data:List <NSString *> list;
List. add (@ "1 ");
List. add (@ "2 generation earthquake army ");
List. add (@ "333"). add (@ "44444"). add (@ "5 ");

Or use the following method:NSArray * array = [[NSArray alloc] initWithObjects:
@ "One", @ "Two", @ "Three", @ "Four", nil];
List <NSString *> list1 (array );

Determine whether a data exists:NSString * del = @" 44444 ";
Bool iscontains = list. contains (del );

Delete data:List. removeAt (2 );
List. remove (del );

Traversal:For (List <NSString * >:: iterator it = list. begin (); it! = List. end (); it ++)
Cout <[(* it) UTF8String] <"";

Or use foreach:_ Block NSString * str;
For_each (list. begin (), list. end (), ^ (NSString * value ){
Str = value;
Std: cout <[value UTF8String] <endl;
});

Obtain the specified index record:NSString * result = list [0];

The code is simple.

Well, today's content will come here first.

Link: http://www.cnblogs.com/daizhj/archive/2012/11/07/2758843.html

Author: daizhj, Dai zhenjun
Weibo: http://weibo.com/daizhj
Tags: ios, c ++, NSArray, NSMutableArray, vector

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.