stl C++ 之间的映射 和 C# 容器

发布于 2024-07-16 18:13:26 字数 147 浏览 4 评论 0原文

有人可以指出常用的 C++ STL 容器(例如向量、列表、映射、集合、多重映射...)与 C# 通用容器之间的良好映射吗?

我已经习惯了前者,并且不知何故我已经习惯了用这些容器来表达算法。 我很难找到与这些等效的 C# 语言。

谢谢你!

Can someone point out a good mapping between the usual C++ STL containers such as vector, list, map, set, multimap... and the C# generic containers?

I'm used to the former ones and somehow I've accustomed myself to express algorithms in terms of those containers. I'm having some hard time finding the C# equivalent to those.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

め可乐爱微笑 2024-07-23 18:13:26

这是一个粗略等价物:

  1. Dictionary <=> unordered_map
  2. HashSet <=> unordered_set
  3. List <=> 向量
  4. LinkedList <=> list

.NET BCL(基类库)没有红黑树(stl map)或优先级队列(make_heap()、push_heap()、pop_heap())。

.NET 集合不像 C++ 那样使用“迭代器”。 它们都实现了 IEnumerable,并且可以使用“foreach 语句”进行迭代。 如果您想手动控制迭代,可以在集合上调用“GetEnumerator()”,这将返回一个 IEnumerator 对象。 IEnumerator.MoveNext() 大致相当于 C++ 迭代器上的“++”,“Current”大致相当于指针引用运算符(“*”)。

C# 确实有一个称为“迭代器”的语言功能。 然而,它们与 STL 中的“迭代器对象”不同。 相反,它们是一种允许自动实现 IEnumerable 的语言功能。 有关更多信息,请参阅 yield returnyield break 语句的文档。

Here's a rough equivalence:

  1. Dictionary<K,V> <=> unordered_map<K,V>
  2. HashSet<T> <=> unordered_set<T>
  3. List<T> <=> vector<T>
  4. LinkedList<T> <=> list<T>

The .NET BCL (base class library) does not have red-black trees (stl map) or priority queues (make_heap(), push_heap(), pop_heap()).

.NET collections don't use "iterators" the way C++ does. They all implement IEnumerable<T>, and can be iterated over using the "foreach statement". If you want to manually control iteration you can call "GetEnumerator()" on the collection which will return an IEnumerator<T> objet. IEnumerator<T>.MoveNext() is roughly equivalent to "++" on a C++ iterator, and "Current" is roughly equivalent to the pointer-deference operator ("*").

C# does have a language feature called "iterators". They are not the same as "iterator objects" in the STL, however. Instead, they are a language feature that allows for automatic implementation of IEnumerable<T>. See documentation for the yield return and yield break statements for more information.

酒几许 2024-07-23 18:13:26

您可能还想看看 STL/CLR 这是

...是一个包装
标准模板库 (STL)
标准 C++ 库的子集,
用于 C++ 和 .NET
框架公共语言运行时
(CLR)。 通过STL/CLR,您可以使用所有
容器、迭代器和
托管中的STL算法
环境。

另外,请记住,您可以使用 /clr 标志编译现有的 C++/STL 代码。

You may also want to take a look at STL/CLR which is

... is a packaging of
the Standard Template Library (STL), a
subset of the Standard C++ Library,
for use with C++ and the .NET
Framework common language runtime
(CLR). With STL/CLR, you can use all
the containers, iterators, and
algorithms of STL in a managed
environment.

Also, keep in mind that you can compile your existing C++/STL code with the /clr flag.

千寻… 2024-07-23 18:13:26

这个SorceForge 项目 看起来是一个有趣的资源,适合您寻找的内容。

This SorceForge project looks like an interesting resource for what your looking for.

口干舌燥 2024-07-23 18:13:26

没有很好的直接映射,因为例如C++ set 和map 使用比较器,而.Net HashSet 和Dictionary 使用哈希码。

There isn't a terrific direct mapping, since e.g. C++ set and map use comparators, whereas .Net HashSet and Dictionary use hash codes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文