作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如果我使用 LINQ 执行以下操作:
void DoSomeStuffWithHashSet()
{
HashSet<int> set = new HashSet<int>();
for (int i = 0; i < 100; ++i) set.Add(i);
if (Lookup(set, new Random().NextInt(200))
System.Console.WriteLine("Yey");
else
System.Console.WriteLine("Ney");
}
bool Lookup(IEnumerable<int> haystack, int needle)
{
// O(N) search or HashSet<int>.Contains()?
return Enumerable.Contains(collection, needle);
}
Enumerable.Contains()
会解析为 HashSet
上的优化实现,还是会执行一个简单的搜索而不考虑输入?
最佳答案
是的,它将使用 HashSet<T>.Contains
. HashSet<T>
工具 ICollection<T>
并根据 documentation for Enumerable.Contains
:
If the type of source implements
ICollection<T>
, theContains
method in that implementation is invoked to obtain the result. Otherwise, this method determines whether source contains the specified element.
总是,总是,总是检查文档!
关于c# - LINQ 会根据输入类型使用专门/优化版本的函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6312232/
我有一个带有模板函数的基类,该函数具有通用模板类型和专用版本。 #ifndef BASE_CLASS #define BASE_CLASS #include using namespace std;
我有这个 3D vector 模板 template class Vec3TYPE{ public: union{ struct{ TYPE x,y,z; }; struct{ TY
我是一名优秀的程序员,十分优秀!