热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

利用NSArrays实现集合的交集与并集操作-ImplementingSetOperationswithNSArrays:IntersectionandUnion

在本文中,我们探讨了如何使用NSArrays来实现集合的交集与并集操作。通过两个示例数组A和B,其中包含一些共同元素(例如A:1,2,3和B:2,3,4),我们将详细介绍如何高效地进行这些集合操作。此外,我们还将讨论这些方法在实际应用中的性能优势和适用场景。

I have two NSArrays A and B that share some common elements, e.g.

我有两个nsarray A和B它们有一些共同的元素。

A: 1,2,3,4,5 
B: 4,5,6,7

I would like to create a new NSArray consisting of the contents common between the two NSArrays joined with the contents of the second NSArray while maintaining the order of the elements and removing duplicates. That is, I would like (A ∩ B) ∪ B.

我想创建一个新的NSArray,它包含两个NSArray之间的共同内容,与第二个NSArray的内容相连接,同时保持元素的顺序并删除重复的内容。也就是说,我想(∩B)∪B。

The operation on the previous NSArrays would yield:

对前一次nsarray的操作将产生:

A ∩ B: 4,5
(A ∩ B) ∪ B: 4,5,6,7

How do I accomplish this in Objective-C?

如何在Objective-C中实现?

5 个解决方案

#1


21  

Convert the NSArrays to NSSets, the standard set operations are available.

将nsarray转换为nsset,可以使用标准的set操作。

NSArray *a = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", @"5", nil];
NSArray *b = [NSArray arrayWithObjects:@"4", @"5", @"6", @"7", nil];

NSMutableSet *setA = [NSMutableSet setWithArray:a];
NSSet *setB = [NSSet setWithArray:b];
[setA intersectSet:setB];
NSLog(@"c: %@", [setA allObjects]);

NSLog output: c: (4, 5)

NSLog输出:c:(4,5)

[setA unionSet:setB];
NSLog(@"d: %@", [setA allObjects]);

NSLog output: d: (6, 4, 7, 5)

NSLog输出:d:(6,4,7,5)

#2


15  

As others have suggested, you can easily do this with NSSet. However, this will not preserve ordering.

正如其他人所建议的,您可以轻松地使用NSSet实现这一点。然而,这并不能保持顺序。

If you want to preserve ordering and you can target OS X 10.7+, then you can use the new NSOrderedSet (and mutable subclass) to do the same thing.

如果您想保持排序,并且您可以针对OS X 10.7+,那么您可以使用新的NSOrderedSet(和mutablecsubclass)来做同样的事情。

#3


6  

By using NSSet, as others have pointed out. For

正如其他人所指出的,通过使用NSSet。为

NSArray * a = [NSArray arrayWithObjects: ... ];
NSArray * b = [NSArray arratWithObjects: ... ];
NSMutableSet * set = [NSMutableSet setWithArray:a];
[set intersectSet:[NSSet setWithArray:b];
[set unionSet:[NSSet setWithArray:b];

This takes care of dupes but won't preserve order. You'd take the results in "set" and sort them back into an array. There's no native collection functionality that will do it all-- if you prefer to keep the order and worry about dupes separately, use NSMutableArray's -removeObjectsInArray: method, etc.

这可以解决问题,但不能维持秩序。您将在“set”中获取结果并将其排序到一个数组中。如果您更倾向于保持订单和对dupes的担心,则使用NSMutableArray的-removeObjectsInArray:方法,等等。

#4


1  

(A ∩ B) ∪ B will always give you B, so this is a pretty bizarre thing to want to calculate. It's like saying "Give me the set of all cars that are colored green, combined with the set of all cars". That's going to give you the set of all cars.

(∩B)∪B将永远给你,这是一个很奇怪的事情要计算。这就像说“给我一套绿色的车,再加上所有的车”。它会给出所有汽车的集合。

#5


0  

There is an NSSet class you can use to perform these operations.

有一个NSSet类可以用来执行这些操作。


推荐阅读
author-avatar
你有小号我就不能有吗_477
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有