开发者

An Analog of List.h in .Net

开发者 https://www.devze.com 2022-12-17 17:06 出处:网络
I used to 开发者_如何学运维use List.h to work with lists in C++, but are there any similar libraries in .Net ? Becouse I can\'t use List.h for managed types.Check out the System.Collections and System

I used to 开发者_如何学运维use List.h to work with lists in C++, but are there any similar libraries in .Net ? Becouse I can't use List.h for managed types.


Check out the System.Collections and System.Collections.Generic namespaces. There, you'll find classes like ArrayList, List<T>, etc...


Here is an example of using List:

List<string> myList = new List<string>();
myList.Add("Item1");
myList.Add("Item2");
myList.ForEach(s => Console.WriteLine(s.ToString()));

and here is an example of LinkedList:

LinkedList<int> myList = new LinkedList<int>();

myList.AddFirst(14);             
myList.AddLast(20);                            
myList.AddLast(34);             
myList.AddBefore(myList.Last, 65); 

LinkedListNode<int> myNode = myList.First;
while (myNode != null)
{
  Console.WriteLine(myNode.Value);
  myNode = myNode.Next;
}


System.Collections.Generic.LinkedList<T> achieves the same funcionality.


If you're referring to the STL list then you'll want the the LinkedList generic class found in System.Collections.Generics

The .Net List generic class is more like an STL vector

0

精彩评论

暂无评论...
验证码 换一张
取 消