When C ++ encounters IOS application development-Dict set

Source: Internet
Author: User


In Object-c, the dictionary (KEY/VALUE) uses NSDictionary and NSMutableDictionary (variable length ). The syntax is as follows:
[Cpp]
NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys: @ "value1", @ "key1", @ "value2", @ "key1", nil]; // end with nil


Method for retrieving elements:
[Cpp]
NSString * value = [myDictionary objectForKey: @ "key1"];


This statement is too complex. So I encapsulated the map class in C ++.

[Cpp]
# Ifndef Discuz2_Maps_h
# Define Discuz2_Maps_h
 
# Include <map>

// Comparator
Template <class key_t>
Struct dict_compare {
Bool operator () (key_t x, key_t y ){
Return x <y ;}
};

// Only for NSString * Comparator
Template <>
Struct dict_compare <NSString *>
{
Bool operator () (NSString * _ x, NSString * _ y ){
String x = [_ x UTF8String];
String y = [_ y UTF8String];
// Std: cout <x <"" <y <endl;
Return x <y ;}
};
 
 
# Define RELEASE_SAFELY (_ POINTER) {[_ POINTER release]; _ POINTER = nil ;}
# Define RETAIN_SAFELY (_ POINTER) {[_ POINTER retain];}

Template <typename K, typename V, typename _ Compare = dict_compare <K>,
Typename _ Alloc = std: allocator <std: pair <const K, V>
Class Dict: public map <K, V, _ Compare, _ Alloc>
{
Private:
Typedef typename std: map <K, V >:: iterator dict_it;

Public:
Dict (){}

Dict (NSArray * array ){
CopyFromArray (array );
}

~ Dict ()
{
Std: cout <"dict destroy! "<Endl;
This-> clear ();
}

Dict & add (const K key, const V value ){
This-> insert (std: make_pair (key, value ));
// RETAIN_SAFELY (value); OBJECT-C object type only
Return (* this );
}

Const V get (const K key ){
Dict_it it = this-> find (key );
If (it! = This-> end ())
Return it-> second;
Else
Return nil;
}

BOOL contains (const K key ){
Return this-> find (key) = this-> end ()? NO: YES;
}

/*
* Returns the VALUE of the specified KEY.
*/
Const V operator [] (const K key)
{
Return this-> get (key );
}

Void remove (const K key ){
This-> erase (key );
}

Void clear (){
For_each (this-> begin (), this-> end (), ^ (std: pair <K, V> pair ){
// RELEASE_SAFELY (pair. second); OBJECT-C object type only
This-> erase (pair. first );
});
}

Void copyFromArray (NSArray * array ){
If ([array count] % 2 = 0) {// The number of elements must be an even number.
For (int I = 0; I <[array count]; I ++ ){
K key = (K) [array objectAtIndex: I];
V value = (V) [array objectAtIndex: ++ I];
This-> add (key, value );
}
}
}
};
# Endif

We can see that the definition comparator method is used to specify the comparison method when the KEY type is NSString. The reason for this is that when you look for a KEY (NSString type), the data in the map comparator may become longer characters and the longer part of the content is garbled. This will cause no results to be found. Of course, this will not cause performance loss. It has not been tested yet. Interested friends can help with the test.


Next, let's take a look at the specific usage.

Instantiate the object and add data:
[Cpp]
Dict <NSString *, NSString *> dict;
Dict. add (@ "Dai zhenjun 1", @ "111111"). add (@ "Dai zhenjun 2", @ "222222 ");


Or use the following method:
[Cpp]
NSArray * array = [[NSArray alloc] initWithObjects:
@ "One", @ "1", @ "Two", @ "2", @ "Three", @ "3", @ "Four", @ "4 ", nil];
Dict <NSString *, NSString *> dictBatch (array );


Determine whether a data exists:
[Cpp]
BOOL iscontains = dict. contains (@ "Dai zhenjun 3 ");

Number of retrieved records:
[Cpp]
Int size = dictBatch. size ();


Traversal:
[Cpp]
For (std: map <NSString *, NSString *>: iterator it = dict. begin (); it! = Dict. end (); it ++ ){
Cout <[it-> second UTF8String] <"";
}

Or use foreach:
[Cpp]
_ Block NSString * str;
For_each (dict. begin (), dict. end (), ^ (std: pair <NSString *, NSString *> pair ){
Str = pair. first;
Cout <[pair. second UTF8String] <"";
});


Obtain the specified index record:
[Cpp]
NSString * result = dict [@ "Dai zhenjun 2"];


The code is relatively simple, so I won't explain it more.

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.