Objective-c set

Source: Internet
Author: User

Set a set of first-level single-value objects, which can be variable or unchanged. Operations include: searching, adding, and deleting members in a set (common to variable sets), comparing two sets, and calculating the intersection and union of the two sets.

 

The following is a set of code:

# Import <Foundation/NSObject. h> <br/> # import <Foundation/NSSet. h> <br/> # import <Foundation/NSValue. h> <br/> # import <Foundation/NSArray. h> <br/> # import <Foundation/NSAID utoreleasepool. h> <br/> # import <Foundation/NSString. h> </p> <p> // Create an integer object <br/> # define upload BJ (v) [NSNumber numberWithInteger: v] </p> <p> // Add a print method to NSSet with the Pringing category </p> <p> @ interface NSSet (Printing) <br/>-(void) print; <br/> @ end </p> <p> @ implementation NSSet (Printing) <br/>-(void) print <br/>{< br/> printf ("{"); </p> <p> for (int I = 0; I <[self allObjects] count]; I ++) <br/> {<br/> printf ("% Ii", (long) [[self allObjects] objectAtIndex: I] integerValue]); <br/>}< br/> // for (NSNumber * element in self) <br/> // printf ("% Ii", (long) [element integerValue]); </p> <p> printf ("}/n "); <br/>}< br/> @ end </p> <p> int main (int argc, const char * argv []) <br/>{< br/> nutoreleasepool * pool = [[nutoreleasepool alloc] init]; </p> <p> NSMutableSet * set1 = [NSMutableSet setWithObjects: <br/> BJ (1), BJ (3), BJ (5), BJ (10), nil]; </p> <p> NSMutableSet * set2 = [NSMutableSet setWithObjects: <br/> describj (-5), describj (100), describj (3), describj (5 ), nil]; </p> <p> NSMutableSet * set3 = [NSMutableSet setWithObjects: <br/> describj (12), describj (200), describj (3), nil]; </p> <p> NSLog (@ "set1:"); <br/> [set1 print]; <br/> NSLog (@ "set2 :"); <br/> [set2 print]; </p> <p> // integrity test <br/> if ([set1 is1_toset: set2] = YES) <br/> NSLog (@ "set1 equals set2"); <br/> else <br/> NSLog (@ "set1 is not equal to set2 "); </p> <p> // Membership test </p> <p> if ([set1 containsObject: INTOBJ (10)] = YES) <br/> NSLog (@ "set1 contains 10"); <br/> else <br/> NSLog (@ "set1 does not contain 10 "); </p> <p> if ([set2 containsObject: INTOBJ (10)] = YES) <br/> NSLog (@ "set2 contains 10 "); <br/> else <br/> NSLog (@ "set2 does not contain 10 "); </p> <p> // add and remove objects from mutable set set1 <br/> [set1 addObject: describj (4)]; <br/> [set1 removeObject: describj (10)]; <br/> NSLog (@ "set1 after adding 4 and removing 10:"); <br/> [set1 print]; </p> <p> // get intersection of two sets <br/> [set1 intersectSet: set2]; <br/> NSLog (@ "set1 intersect set2 :"); <br/> [set1 print]; </p> <p> // union of two sets <br/> [set1 unionSet: set3]; <br/> NSLog (@ "set1 union set3:"); <br/> [set1 print]; </p> <p> [pool drain]; <br/> return 0; </p> <p >}< br/>

Related Article

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.