Mapping between stl C++ and C# containers(stl C++ 和 C# 容器之间的映射)
问题描述
有人能指出常用的 C++ STL 容器(如 vector、list、map、set、multimap...)和 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?
我已经习惯了前者,并且不知何故我已经习惯于用这些容器来表达算法.我很难找到与这些等效的 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.
谢谢!
推荐答案
这是一个粗略的等价:
字典<K,V><=>unordered_map<K,V>HashSet<T><=>unordered_set<T>列表<T><=>向量<T>LinkedList<T><=>list<T>
Dictionary<K,V><=>unordered_map<K,V>HashSet<T><=>unordered_set<T>List<T><=>vector<T>LinkedList<T><=>list<T>
.NET BCL(基类库)没有红黑树(stl map)或优先级队列(make_heap()、push_heap()、pop_heap()).
The .NET BCL (base class library) does not have red-black trees (stl map) or priority queues (make_heap(), push_heap(), pop_heap()).
.NET 集合不像 C++ 那样使用迭代器".它们都实现了 IEnumerable<T>,并且可以使用foreach 语句"进行迭代.如果你想手动控制迭代,你可以在集合上调用GetEnumerator()",这将返回一个IEnumeratorIEnumerator 大致相当于 C++ 迭代器上的++",Current"大致相当于指针引用运算符 (*").
.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# 确实有一个称为迭代器"的语言特性.但是,它们与 STL 中的迭代器对象"不同.相反,它们是一种允许自动实现 IEnumerable<T> 的语言特性.有关详细信息,请参阅 yield return 和 yield break 语句的文档.
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.
这篇关于stl C++ 和 C# 容器之间的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:stl C++ 和 C# 容器之间的映射
基础教程推荐
- Moq It.Is<>不匹配 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
