In Object-c, the array uses NSArray and NSMutableArray (Variable Length array ). The syntax is as follows:
[Cpp]
NSArray * array = [[NSArray alloc] initWithObjects: @ "One", @ "Two", @ "Three", @ "Four", nil];
Method for retrieving array elements:
[Html]
[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: www.2cto.com.
[Cpp]
# 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:
[Cpp]
List <NSString *> list;
List. add (@ "1 ");
List. add (@ "2 generation earthquake army ");
List. add (@ "333"). add (@ "44444"). add (@ "5 ");
Or use the following method:
[Cpp]
NSArray * array = [[NSArray alloc] initWithObjects:
@ "One", @ "Two", @ "Three", @ "Four", nil];
List <NSString *> list1 (array );
Determine whether a data exists:
[Cpp]
NSString * del = @" 44444 ";
Bool iscontains = list. contains (del );
Delete data:
[Cpp]
List. removeAt (2 );
List. remove (del );
Traversal:
[Cpp]
For (List <NSString * >:: iterator it = list. begin (); it! = List. end (); it ++)
Cout <[(* it) UTF8String] <"";
Or use foreach:
[Cpp]
_ Block NSString * str;
For_each (list. begin (), list. end (), ^ (NSString * value ){
Str = value;
Std: cout <[value UTF8String] <endl;
});
Obtain the specified index record:
[Cpp]
NSString * result = list [0];