在 C# 中使用列表

发布于 2024-07-09 11:50:51 字数 137 浏览 5 评论 0原文

我是一名高级软件工程学生,目前正在上数据结构和算法课程。 我们的教授希望我们使用 C++ STL 中的列表结构编写一个程序。 我一直在尝试越来越多地使用 C#,并且想知道 .NET 中的 ArrayList 结构是否可以很好地替代 STL List 实现。

I am an upper level Software Engineering student currently in a Data Structures and Algorithms class. Our professor wants us to write a program using the List structure found in the C++ STL. I have been trying to use C# more and more, and was wondering if the ArrayList structure in .NET is a good substitute for the STL List implementation.

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

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

发布评论

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

评论(7

方圜几里 2024-07-16 11:50:52

如果 STL List 使用模板,您可能需要查看 System.Collections.Generic 中的通用 List 类。

if the STL List uses templates, you might want to look at the generic List class in System.Collections.Generic.

南薇 2024-07-16 11:50:52

嗯,C++ STL 没有称为“List”的结构。 我认为有一个“列表”,它是一个链表。 相比之下,C# 的 List 类似于 C++ 的向量。

Um, C++ STL doesn't have a structure called "List". I think there is a "list", which is a linked list. C#'s List, in contrast, is analagous to C++'s vector.

暖伴 2024-07-16 11:50:52

ArrayList 类在某种程度上已被弃用。 从 .NET 1.0 时代开始,泛型还不存在。

您应该使用 System.Collections.Generic.List 代替。 像这样:

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
System.Console.WriteLine(myList[0]);

是的,它们都是很好的替代品。 不过,您应该使用通用列表,因为它是类型安全的并且可能更快。

The ArrayList class is somewhat deprecated. It is from the .NET 1.0 times when generics did not exist yet.

You should use System.Collections.Generic.List instead. Like this:

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);
System.Console.WriteLine(myList[0]);

And yes, both of them are good substitutes. You should use the generic List though, since it is type safe and potentially quicker.

妥活 2024-07-16 11:50:52

谢谢大家

quertie,我打错了,意思是列表而不是列表...

作业是使用 std::list 使用简单结构列表添加多项式,该结构将保存系数和 x 的幂...简单足够了,我知道,但由于该类据称是独立于语言的,所以我想尝试使用 c#

Thanks everyone

quertie, i mistyped and meant list instead of List...

the assignment is to use std::list to add polynomials using a list of simple structs, a struct that would hold the coefficient and the power of x...easy enough, I know, but since the class is supposedly language-independent, I wanted to try to use c#

鸠魁 2024-07-16 11:50:52

std::list 最接近的 C# 类似物是 System.Collections.List。 两者都是通用集合,并实现标准列表类型操作。

The closest C# analogue of the std::list is System.Collections.List. Both are generic collections, and implement the standard list-type actions.

森林散布 2024-07-16 11:50:51

除非您坚持使用 .NET 1.1,否则请使用 List 而不是 ArrayList。 但你根本上关心的是什么呢? 假设您没有 List 可供引用 - 您需要适当的数据结构来做什么?

Unless you're stuck with .NET 1.1, use List<T> instead of ArrayList. But what are you fundamentally concerned about? Suppose you didn't have List to refer to - what do you need the appropriate data structure to do?

随波逐流 2024-07-16 11:50:51

你自己应该能够回答这个问题。 STL列表使用的实现策略是什么? ArrayList 是什么? 同样,STL列表所呈现的抽象API是什么(就提供的操作而言)? 将此与 STL 列表进行比较:一个提供了另一个没有提供的内容?

You should be able to answer this question yourself. What is the implementation strategy used in STL lists? What is the one of ArrayList? Likewise, what is the abstract API presented by STL list (in terms of operations provided)? Compare this to STL List: what does the one provide that the other doesn't?

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