滚动你自己的std :: vector ??? [英] Roll your own std::vector ???
问题描述
我需要几个自定义类的std :: vector like功能。我已经在没有Boxing的名为ArrayList的线程中广泛讨论了这个问题,并且
拆箱。解决方案是为每个自定义类创建非泛型(非C ++模板)
std :: vector like功能。 (解决方案必须
在Visual Studio 2002中工作)。
因为我已经为YeOlde C ++编译器编写了一个std :: vector(Borland
C ++ 1.0)既没有模板也没有STL,我知道怎么做。我不知道怎么做才能直接在垃圾收集的C#中重新分配内存。
我写了我需要的东西伪代码,
的正确C#语法是什么?
int size;
int容量;
bool AppendDataItem(DataItemType Data){
if(size == Capacity){
(1)Capacity = Capacity * 2; //或* 1.5
(2)Temp = MemoryPointer;
(3)MemoryPointer =分配(容量);
(4)复制从Temp到MemoryPointer的数据;
(5)DeAllocate(Temp);
(6)MemoryPointer [Size] =数据;
(7 )尺寸++;
}
}
" Peter Olcott" ; < No **** @SeeScreen.comaécritdansle message de news:
wJ*******************@newsfe18.lga ...
|我需要std :: vector类似于几个自定义类的功能。
|我已经写了我需要的伪代码,什么是正确的C#语法
for
|这个?
在C#中,通常不应手动释放内存,因为这会降低
GC的速度。
如果你真的不能或不想使用通用List< Tclass,那么
会更快更容易,那么试试这段代码,我认为它实现了你的目标>
想要:
公共课测试
{
private int size = 0;
int [] ia = new int [4];
public void AppendDataItem(int data)
{
if(size == ia.Length)
Array.Resize(ref ia,ia.Length * 2);
ia [size ++ ] =数据;
}
}
{
测试t =新测试();
for(int i = 0; i< 10; i ++)
t.AppendDataItem(i);
}
Joanna
-
Joanna Carter [TeamB]
顾问软件工程师
" Joanna Carter [TeamB]" < jo **** @not.for.spamaécritdansle message de
news: ee ************** @ TK2MSFTNGP04.phx.gbl ...
|如果你真的不能或不想使用通用List< Tclass,那么
|会更快更容易,然后尝试这个代码,我认为它实现了什么
你
|想:
对不起,我使用了只在.NET 2.0中提供的array.Resize(...);你好
说你想要.NET 1兼容性,之前你需要使用Copy或
CopyTo:
|公共课测试
| {
| private int size = 0;
|
| int [] ia = new int [4];
|
| public void AppendDataItem(int data)
| {
if(size == ia.Length)
{
int [] temp = new int [ia.Length * 2];
Array.Copy(ia,temp,ia.Length);
ia = temp;
}
ia [size ++] =数据;
| }
| }
Joanna
-
Joanna Carter [TeamB]
顾问软件工程师
" Joanna Carter [TeamB]" < jo **** @ not.for.spamwrote in message
news:%2 *************** @ TK2MSFTNGP06.phx.gbl .. 。
" Joanna Carter [TeamB]" < jo **** @not.for.spamaécritdansle message de
news: ee ************** @ TK2MSFTNGP04.phx.gbl ...
|如果你真的不能或不想使用通用List< Tclass,那么
|会更快更容易,然后尝试这个代码,我认为它实现了什么
你
|想:
对不起,我使用了只在.NET 2.0中提供的array.Resize(...);你好
说你想要.NET 1兼容性,之前你需要使用Copy或
CopyTo:
|公共课测试
| {
| private int size = 0;
|
| int [] ia = new int [4];
我假设你在这里分配四个整数,并且我可以轻松地分配一个整数。
< blockquote class =post_quotes>
|
| public void AppendDataItem(int data)
| {
if(size == ia.Length)
{
int [] temp = new int [ia.Length * 2];
Array.Copy(ia,temp,ia.Length);
如果我理解正确的话,我们可以使用
这个陈述来改善性能:
Array.Copy(ia,temp,size);
我说错了吗? (目的地,来源,长度)???
>
ia = temp;
这就像C ++中的指针赋值???
}
ia [size ++] = data;
| }
| }
Joanna
-
Joanna Carter [TeamB]
顾问软件工程师
I need std::vector like capability for several custom classes. I already
discussed this extensively in the thread named ArrayList without Boxing and
Unboxing. The solution was to simply create non-generic (non C++ template)
std::vector like capability for each of these custom classes. (Solution must
work in Visual Studio 2002).
Since I have already written one std::vector for a YeOlde C++ compiler (Borland
C++ 1.0) that had neither templates nor STL, I know how to do this. What I don''t
know how to do is to directly re-allocate memory in the garbage collected C#.
I have written what I need in pseudocode, what is the correct C# syntax for
this?
int Size;
int Capacity;
bool AppendDataItem(DataItemType Data) {
if (Size == Capacity) {
(1) Capacity = Capacity * 2; // Or * 1.5
(2) Temp = MemoryPointer;
(3) MemoryPointer = Allocate(Capacity);
(4) Copy Data from Temp to MemoryPointer;
(5) DeAllocate(Temp);
(6) MemoryPointer[Size] = Data;
(7) Size++;
}
}
"Peter Olcott" <No****@SeeScreen.coma écrit dans le message de news:
wJ*******************@newsfe18.lga...
|I need std::vector like capability for several custom classes.
| I have written what I need in pseudocode, what is the correct C# syntax
for
| this?
In C#, should not normally deallocate memory manually as this slows down the
GC.
If you really can''t or don''t want to use the generic List<Tclass, which
would be faster and easier, then try this code, I think it achieves what you
want :
public class Test
{
private int size = 0;
int[] ia = new int[4];
public void AppendDataItem(int data)
{
if (size == ia.Length)
Array.Resize(ref ia, ia.Length * 2);
ia[size++] = data;
}
}
{
Test t = new Test();
for (int i = 0; i < 10; i++ )
t.AppendDataItem(i);
}
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
"Joanna Carter [TeamB]" <jo****@not.for.spama écrit dans le message de
news: ee**************@TK2MSFTNGP04.phx.gbl...
| If you really can''t or don''t want to use the generic List<Tclass, which
| would be faster and easier, then try this code, I think it achieves what
you
| want :
Sorry, I used array.Resize(...) which is only available in .NET 2.0; you
said you wanted .NET 1 compatibilty, thyerefore you need to use Copy or
CopyTo :
| public class Test
| {
| private int size = 0;
|
| int[] ia = new int[4];
|
| public void AppendDataItem(int data)
| {
if (size == ia.Length)
{
int[] temp = new int[ia.Length * 2];
Array.Copy(ia, temp, ia.Length);
ia = temp;
}
ia[size++] = data;
| }
| }
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
"Joanna Carter [TeamB]" <jo****@not.for.spamwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl..."Joanna Carter [TeamB]" <jo****@not.for.spama écrit dans le message de
news: ee**************@TK2MSFTNGP04.phx.gbl...
| If you really can''t or don''t want to use the generic List<Tclass, which
| would be faster and easier, then try this code, I think it achieves what
you
| want :
Sorry, I used array.Resize(...) which is only available in .NET 2.0; you
said you wanted .NET 1 compatibilty, thyerefore you need to use Copy or
CopyTo :
| public class Test
| {
| private int size = 0;
|
| int[] ia = new int[4];I am assuming that you are allocating four integers here, and that I could just
as easily allocate one.
|
| public void AppendDataItem(int data)
| {
if (size == ia.Length)
{
int[] temp = new int[ia.Length * 2];
Array.Copy(ia, temp, ia.Length);If I understand this correctly, we could improve the performance a little using
this statement instead:
Array.Copy(ia, temp, size);
Am I correct? (Destination, Source, Length) ???
>
ia = temp;Is this like a pointer assignment in C++ ???
}
ia[size++] = data;
| }
| }
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
这篇关于滚动你自己的std :: vector ???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!