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# 容器之间的映射
基础教程推荐
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 如果条件可以为空 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 将数据集转换为列表 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
