简体   繁体   English

C#等价的C ++向量,具有连续的内存?

[英]C# equivalent of C++ vector, with contiguous memory?

What's the C# equivalent of C++ vector? 什么是C#等价的C ++向量?

I am searching for this feature: 我正在寻找这个功能:

To have a dynamic array of contiguously stored memory that has no performance penalty for access vs. standard arrays. 拥有连续存储内存的动态数组,对访问与标准数组没有性能损失。

I was searching and they say .NET equivalent to the vector in C++ is the ArrayList , so: 我在搜索,他们说.NET equivalent to the vector in C++ is the ArrayList ,所以:

Do ArrayList have that contiguous memory feature? ArrayList是否具有连续的内存功能?

You could use a List<T> and when T is a value type it will be allocated in contiguous memory which would not be the case if T is a reference type. 您可以使用List<T> ,当T是值类型时,它将被分配在连续的内存中,如果T是引用类型,则不会是这种情况。

Example: 例:

List<int> integers = new List<int>();
integers.Add(1);
integers.Add(4);
integers.Add(7);

int someElement = integers[1];

use List<T> . 使用List<T> Internally it uses arrays and arrays do use contiguous memory. 在内部它使用数组,并且数组确实使用连续的内存。

First of all, stay away from Arraylist or Hashtable . 首先,远离ArraylistHashtable Those classes are to be considered deprecated, in favor of generics. 这些类被认为是不赞成的,有利于泛型。 They are still in the language for legacy purposes. 它们仍然是用于遗留目的的语言。

Now, what you are looking for is the List<T> class. 现在,您要找的是List<T>类。 Note that if T is a value type you will have contiguos memory, but not if T is a reference type, for obvious reasons. 请注意,如果T是值类型,您将具有连续内存,但是如果T是引用类型则不会,原因显而易见。

C# has a lot of reference types. C#有很多引用类型。 Even if a container stores the references contiguously, the objects themselves may be scattered through the heap 即使容器连续存储引用 ,对象本身也可能分散在堆中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM