zoukankan      html  css  js  c++  java
  • 常用基础OC 集合

     //    2016071917:50:53    集合

            

            

            //七、NSSet 集合对象(容器类,)

            

            

            //  1. 使用类方法创建对象

            

            NSSet *set1 = [NSSet set];  //  创建一个空的集合对象

            

            NSSet *set2 = [NSSet setWithObject:@"abc"];

            

            NSSet *set3 = [NSSet setWithObjects:@"abc", @"aaa", @"bbb", nil];

            

            NSLog(@"%@", set3);

            

            

            

            NSArray *array = [NSArray arrayWithObjects:@"a",@"b", @"c", nil];

            

            NSSet *set4 = [NSSet setWithArray:array];   //  使用数组创建

            

            NSLog(@"%@", set4);

            

            

            

            NSSet *set5 = [NSSet setWithSet:set4];      //  使用集合创建

            

            NSLog(@"%@", set5);

            

            

            

            //  2.使用实例方法创建

            

            NSSet *set6 = [[NSSet alloc] init];

            

            NSLog(@"%@", set6);

            

            NSSet *set7 = [[NSSet alloc] initWithObjects:@"hello", @"hhaa", @"bbjdh", nil];

            

            NSLog(@"%@", set7);

            

            NSSet *set8 = [[NSSet alloc] initWithArray:array];

            

            NSLog(@"%@", set8);

            

            NSSet *set9 = [[NSSet alloc] initWithSet:set7];

            

            NSLog(@"%@", set9);

            

            

            

            //  3.返回几个元素个数

            

            NSLog(@"%ld", [set7 count]);

            

            

            

            //  4.枚举器访问集合元素

            

            NSEnumerator *enumerator = [set7 objectEnumerator];

            

            NSString *str = nil;

            

            while (str = [enumerator nextObject]) {

                

                NSLog(@"%@", str);

                

            }

            

            

            

            //  5.判断两个几个是否有交集

            

            BOOL ifhasIntersection = [set2 intersectsSet:set3];

            

            NSLog(@"%d", ifhasIntersection);

            

            

            

            //  6.判断两个集合是否相等

            

            NSLog(@"%d", [set2 isEqualToSet:set3]);

            

            

            

            //  7.判断当前集合是否是子集

            

            NSLog(@"%d", [set2 isSubsetOfSet:set3]);

            

            

            

            //5.2可变集合 NSMutableSet

            

            //  创建指定元素个数的一个集合对象

            

            NSMutableSet *mutableSet = [NSMutableSet setWithCapacity:4];

            

            [mutableSet addObject:@"aaa"];

            

            NSLog(@"%@", mutableSet);

            

            //  类方法创建可变集合

            

            NSMutableSet *mutableSet1 = [NSMutableSet setWithObjects:@"aaa", @"bbb", @"ccc", nil];

            

            NSMutableSet *mutableSet2 = [NSMutableSet setWithObject:@"aaa"];

            

            

            

            //  添加一个对象到集合

            

            [mutableSet2 addObject:@"ddd"];

            

            NSLog(@"%@", mutableSet2);

            

            

            

            //  从集合中删除一个对象

            

            [mutableSet2 removeObject:@"ddd"];

            

            NSLog(@"%@", mutableSet2);

            

            

            

            //  把数组对象添加到集合对象中

            

            NSArray *arr10 = [NSArray arrayWithObjects:@"eee", @"fff", nil];

            

            [mutableSet1 addObjectsFromArray:arr10];

            

            NSLog(@"%@", mutableSet1);

            

            

            

            //  得到两个集合的交集 注意intersectSetintersectsSet的区别,后者是判断是否有交集的方法, 返回的是bool

            

            [mutableSet1 intersectSet:mutableSet2];

            

            NSLog(@"%@", mutableSet1);

            

            

            

            //  从一个集合中减去另一个集合

            

            [mutableSet1 minusSet:mutableSet2];

            

            NSLog(@"%@", mutableSet1);

            

            

            

            //  从一个元素中删除所有元素

            

            [mutableSet2 removeAllObjects];

            

            NSLog(@"%@", mutableSet2);

            

            

            

            //  取两个集合的并集

            

            [mutableSet1 unionSet:mutableSet2];

            

            NSLog(@"%@", mutableSet1);

            

            NSLog(@"%@", mutableSet1);

            

            

            

            //  设置给集合赋值

            

            [mutableSet1 setSet:mutableSet2];

            

            NSLog(@"%@", mutableSet1);

  • 相关阅读:
    【BZOJ4517】[SDOI2016] 排列计数(组合数)
    【BZOJ4818】[SDOI2017] 序列计数(矩乘水题)
    【BZOJ4872】[SHOI2017] 分手是祝愿(思维+动态规划)
    【BZOJ4821】[SDOI2017] 相关分析(线段树)
    【BZOJ2710】[Violet 1] 追风者(计算几何)
    【BZOJ3199】[SDOI2013] escape(半平面交+BFS)
    【BZOJ1007】[HNOI2008] 水平可见直线(几何)
    【BZOJ3689】异或之(可持久化Trie树)
    【BZOJ3261】最大异或和(可持久化Trie树水题)
    NOI Online #3 提高组 小记
  • 原文地址:https://www.cnblogs.com/DafaRan/p/5700727.html
Copyright © 2011-2022 走看看