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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/741054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:22:03  来源:igfitidea点击:

Mapping between stl C++ and C# containers

c#c++stlcontainers

提问by tato

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?

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

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.

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

Thank you!

谢谢!

采纳答案by Scott Wisniewski

Here's a roughequivalence:

这是一个粗略的等价:

  1. Dictionary<K,V><=> unordered_map<K,V>
  2. HashSet<T><=> unordered_set<T>
  3. List<T><=> vector<T>
  4. LinkedList<T><=> list<T>
  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 BCL(基类库)没有红黑树(stl 映射)或优先级队列(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 "foreachstatement". 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 ("*").

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

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 returnand yield breakstatements for more information.

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

回答by Brian

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

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

回答by JP Alioto

This SorceForge projectlooks like an interesting resource for what your looking for.

这个 SorceForge 项目看起来像是一个有趣的资源,可以满足您的需求。

回答by Dаn

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

您可能还想看看STL/CLR

... 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.

... 是标准模板库 (STL) 的打包,它是标准 C++ 库的子集,用于 C++ 和 .NET Framework 公共语言运行时 (CLR)。借助 STL/CLR,您可以在托管环境中使用 STL 的所有容器、迭代器和算法。

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

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