NSSet(集合)算法
集合:集合(NSSet)和數組(NSArray)有類似之處,都是存儲不一樣的對象的地址;不過NSArray是有序的集合,NSSet是無序的集合。
集合是一種哈希表,運用散列算法,查找集合中的元素比數組速度更快,可是它沒有順序。數組
集合的初始化spa
int main(int argc, const char * argv[]) { @autoreleasepool { //類方法初始化一個集合 //方法1) //給一個集合賦多個值 NSSet *c = [NSSet setWithObjects:@"one", @"two", @"three", @"four", nil]; //集合是無序的 NSLog(@"%@", c); //方法2) //先建立一個包含元素的數組_d NSArray *_d = [NSArray arrayWithObjects:@"one", @"two", @"three", @"four", @"five", nil]; //將數組_d裏面的全部元素所有傳給集合d NSSet *d = [NSSet setWithArray:_d]; NSLog(@"%@", d); //實例化方法初始化一個集合, 加alloc建立一個空間!!! //方法1) NSSet *e = [[NSSet alloc]init]; //方法2) NSSet *f = [[NSSet alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5", nil ]; //方法3) NSSet *g = [[NSSet alloc]initWithSet:f]; //方法4) NSArray *_h = [NSArray arrayWithObjects:@"6",@"7",@"8",@"9",@"10", nil]; NSSet *h = [[NSSet alloc]initWithArray:_h]; NSLog(@"%@", h); } return 0; }
集合的使用code
int main(int argc, const char * argv[]) { @autoreleasepool { //新建立一個集合 NSSet *a = [NSSet setWithObjects:@"one", @"two", @"three", @"four", @"five", @"six", @"seven", nil]; //打印集合中的全部對象個數 NSLog(@"%lu", (unsigned long)[a count]); //打印集合中任意一個對象 NSLog(@"%@", [a anyObject]); //打印全部對象 NSLog(@"%@", [a allObjects]); } return 0; }
集合的比較orm
int main(int argc, const char * argv[]) { @autoreleasepool { //新建立一個集合a NSSet *a = [NSSet setWithObjects:@"one", @"two", @"three", @"four", @"five", @"six", @"seven", nil]; NSSet *b = [NSSet setWithObjects:@"two", @"one", @"three", @"six", @"five", @"four", @"seven", nil]; //判斷是否是相同的集合,若是是打印1,若是不是打印0 BOOL x1 = [a isEqualToSet:b]; NSLog(@"%hhd", x1); NSSet *c = [NSSet setWithObjects:@"one", @"two", @"three", nil]; NSSet *d = [NSSet setWithObjects:@"one", @"two", nil]; //判斷集合d是否是集合c的子集合,若是是打印1,若是不是打印0 BOOL x2 = [d isSubsetOfSet:c]; NSLog(@"%hhd", x2); NSSet *e = [NSSet setWithObjects:@"1", @"2",@"3", nil]; NSSet *f = [NSSet setWithObjects:@"1", @"a", @"b", @"c", nil]; //判斷有沒有交集,若是有打印1,若是沒有打印0 BOOL x3 = [e intersectsSet:f]; NSLog(@"%hhd", x3); NSSet *g = [NSSet setWithObjects:@"a", @"b", @"c", @"d", nil]; //判斷有沒有集合g裏面有沒有特定元素@"c",若是有打印1,若是沒有打印0 BOOL x4 = [g containsObject:@"c"]; NSLog(@"%hhd", x4); } return 0; }
NSMutableSet(可變集合)對象
可變集合的初始化three
int main(int argc, const char * argv[]) { @autoreleasepool { //初始化一個可變集合 NSMutableSet *a = [[NSMutableSet alloc]initWithObjects:@"a", @"b", @"c", nil]; NSLog(@"%@", a); } return 0; }
可變集合的使用ci
int main(int argc, const char * argv[]) { @autoreleasepool { //建立一個可變集合a NSMutableSet *a = [[NSMutableSet alloc]initWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", @"g", nil]; //建立一個可變集合b NSMutableSet *b = [NSMutableSet setWithObjects:@"a", @"b", @"c", nil]; //從集合a中剔除集合b相同的對象 [a minusSet:b]; NSLog(@"%@", a); NSMutableSet *c = [NSMutableSet setWithObjects:@"a", @"b", nil]; NSMutableSet *d = [NSMutableSet setWithObjects:@"c", @"d", nil]; //將集合d添加到集合c中 [c unionSet:d]; NSLog(@"%@", c); NSMutableSet *e = [NSMutableSet setWithObjects:@"a", @"b", @"c", nil]; NSMutableSet *f = [NSMutableSet setWithObjects:@"a", @"e", @"f", nil]; [e intersectSet:f]; //集合e保留與集合f的交集,其他刪掉 NSLog(@"%@", e); NSMutableSet *g = [NSMutableSet setWithObjects:@"a", @"b", @"c", nil]; //在集合g中添加一個對象@"1" [g addObject:@"1"]; NSLog(@"%@", g); //新建一個數組h NSArray *h = [NSArray arrayWithObjects:@"d", @"e", nil]; //將數組h添加到集合g中 [g addObjectsFromArray:h]; NSLog(@"%@", g); NSMutableSet *i = [NSMutableSet setWithObjects:@"a", @"b", @"c", nil]; //將集合i中的元素@"b"刪掉 [i removeObject:@"b"]; NSLog(@"%@", i); //刪掉集合i中的所有元素 [i removeAllObjects]; NSLog(@"%@", i); //將集合j中的元素根據數組k刪除 //先建立一個可變集合j NSMutableSet *j = [NSMutableSet setWithObjects:@"a", @"b", @"c", @"d", nil]; //建立一個數組k NSArray *k = [NSArray arrayWithObjects:@"a", @"d", nil]; //現根據數組k找出每一個key@"i"對應的value,再判斷集合j裏面有沒有,若是有,則刪掉 for (int i=0; i<k.count; i++) { if ([j containsObject:[k objectAtIndex:i]]) { [j removeObject:[k objectAtIndex:i]]; } } NSLog(@"%@", j); } return 0; }