zoukankan      html  css  js  c++  java
  • Objective-C之集合对象(NSSet,NSMutableSet,NSIndexSet)

    NSArray:有序的集合,NSSet:无序的集合,散列存储。 但是NSSet保证数据的唯一性。当插入相同的数据时,不会有任何效果。从内部实现来说是hash表。NSMutableSet是NSSet的子类,是NSSet的可变形式。

    NSSet、NSMutableSet

    NSSet的使用
    [NSSet setWithSet:(NSSet *)set]; 用另外一个set对象构造
    [NSSet setWithArray:(NSArray *)array];用数组构造
    [NSSet setWithObjects:…]:创建集合对象,并且初始化集合中的数值,结尾必需使用nil标志。
    [set count] ; 得到这个结合对象的长度。
    [set containsObject:…]: 判断这个集合中是否存在传入的对象,返回Bool值。
    [set objectEnumerator]: 将集合放入迭代器。
    [enumerator nextObject]:得到迭代器中的下一个节点数据,使用while遍历这个迭代器,方可遍历集合对象中的对象。
    [set isEqualToSet:objset]:判断两个集合是否完全相等,返回Bool值。
    [set isSubsetOfSet:objset]:判断集合中的所有数据是否都相等与objeset集合中,返回Bool值。
    [set allObjects];

    NSMutableSet继承NSSet,它可以使用NSSet的方法。

    [NSMutableSet setWithCapacity:6]:创建可变集合对象,并且初始化长度为6。
    [set addObject: obj] : 向集合中动态的添加对象。
    [set removeObject:obj]:删除集合中的一个对象。
    [set removeAllObjects]:删除集合中的所有对象。
    [set unionSet:obj]:向集合中添加一个obj集合的所有数据。
    [set minusSet:obj]:向集合中删除一个obj集合的所有数据。
    [set intersectSet]:向集合中删除一个不包含obj集合的所有数据。

    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
    
            //定义一个NSSet
            NSSet* set1 = [NSSet setWithObjects:@"str1",@"str2",@"str3", nil];
    
            //使用NSArray初始化一个NSSet
            NSArray *array = [[NSArray alloc] initWithObjects:@"obj1",@"obj2", @"obj3",@"str1",nil];
            NSSet* set2 = [NSSet setWithArray:array];
    
            //用另外一个set对象构造
            NSSet* set3 = [NSSet setWithSet:set2];
    
            //可变长度的集合
            NSMutableSet* set4 = [NSMutableSet setWithSet:set3];
    
            //在可变集合中添加和移除对象
            [set4 addObject:@"obj4"];
            [set4 removeObject:@"obj2"];
    
            //获取这个结合对象的长度
            NSLog(@"size of set1:%lu",[set1 count]);
    
            //判断是否含有某个对象
            if ([set3 containsObject:@"str1"])
                NSLog(@"set3 包含 str1");
             else
                NSLog(@"set3 不包含 str1");
    
            //判断set1 是否等于set2
            if ([set1 isEqualToSet:set2]) 
                NSLog(@"set1 等于 set2");
            else
                NSLog(@"set1 不等于 set2");
    
            //判断set1 是否是set2的子集合
            if ([set1 isSubsetOfSet:set2])
                NSLog(@"set1 是 set2的子集合");
            else
                NSLog(@"set1 不是 set2的子集合");
    
            //获取两个集合的交集
            [set1 intersectsSet:set2];
    
            //获取两个集合的并集
            [set4 unionSet:set3];
    
            //迭代遍历
            for (NSObject* object in set4) {
                 NSLog(@"set4里的对象:%@", object);
            }
    
            //使用NSEnumerator迭代遍历
            NSEnumerator *enumerator = [set4 objectEnumerator];
            for (NSObject *object in enumerator) {
                NSLog(@"set4里的对象:%@", object);
            }
        }
        return 0;
    }
    NSIndexSet

    NSIndexSet类和它的可变副本(NSMutableIndexSet)表示一个唯一的无符号整数的集合。这个类用于存储有序的索引到某种数据结构。例如,给定一个NSArray对象,你可以使用该数组中的索引,以确定对象的一个子集。

    NSIndexSet *indexSet1 = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 3)];
    
    NSMutableIndexSet *indexSet2 = [NSMutableIndexSet indexSet];
    [indexSet addIndex:2];
    [indexSet addIndex:5];
    [indexSet addIndex:8];
    
    //列出NSIndexSet的值
    unsigned index;
    for (index = [indexSet firstIndex];
    	 index != NSNotFound; index = [indexSet indexGreaterThanIndex: index])  {
    	
    }

    NSIndexSet的一些方法

    +(NSIndexSet) indexSet   创建一个空的索集合
    -(BOOL) containIndex:idx 如果索引集合包含索引idx,则返回YES,否则返回NO
    -(NSUinteger) count 返回索引集合中索引的数量
    -(NSUinteger) firstIndex 返回集合中的第一个索引,如何集合为空,则返回NSNotFound
    -(NSUinteger) indexLessThanIndex:idx 返回集合中小于idx的最接近的索引,如果没有小于idx的索引,则返回NSNotFound.类似indexLessOrEuqalToIndex:、indexGreaterThanIndex和indexGreaterThanOrEqualIndex:
    -(NSIndexSet *) indexesPassingTest:(BOOL)(^)(NSUinteger idx,BOOL *stop) block  区块应用在集合中的每个元素。idx添加到了NSIndexSet中返回YES,否则返回NO。设置指针变量stop为YES,表示中断处理。

  • 相关阅读:
    NOJ-1581 筷子 (线性DP)
    UVA-242 Stamps and Envelope Size (DP)
    POJ 1860 (SPFA判断正环)
    POJ 3268 最短路水题
    STL----priority_queue
    STL----unique
    POJ 2031(最小生成树Kruskal算法+几何判断)
    POJ 3468(线段树区间修改+区间求和)
    学习线段树
    POJ 1251(最小生成树裸题)
  • 原文地址:https://www.cnblogs.com/wangliyuan/p/4740412.html
Copyright © 2011-2022 走看看