减去HashSets(并返回副本)? [英] Subtract HashSets (and return a copy)?
本文介绍了减去HashSets(并返回副本)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个HashSet,
I've got a HashSet,
var universe = new HashSet<int>();
还有一堆子集,
var sets = new List<HashSet<int>>(numSets);
我想减去一个块,我可以这样:
I want to subtract a chunk, which I can do like this:
var remaining = universe.ExceptWith(sets[0]);
但是ExceptWith
是就地工作的.我不想修改universe
.我应该先克隆它,还是有更好的方法?
But ExceptWith
works in-place. I don't want to modify the universe
. Should I clone it first, or is there a better way?
推荐答案
我想我应该克隆它 第一的?我该怎么办?
I guess I should clone it first? How do I do that?
var universe = new HashSet<int>();
var subset = new HashSet<int>();
...
// clone the universe
var remaining = new HashSet<int>(universe);
remaining.ExceptWith(subset);
不像Except
扩展方法那样简单,但可能更快(您应该运行一些性能测试以确保成功)
Not as simple as with the Except
extension method, but probably faster (you should run a few performance tests to make sure)
这篇关于减去HashSets(并返回副本)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文