Is in C# List something like vector.reserve(n) in C++

痴心易碎 提交于 2019-12-10 16:01:09

问题


When adding a lot of elements in System.Collections.Generic.List<T> it is running slow because when nums increases capacity it must copy all elements. In C++ this is fixed with vector.reserve(n). How can i do that in C#?


回答1:


Use Capacity property:

list.Capacity = n;

or you can set initial capacity via the constructor:

var list = new List<int>(n);


来源:https://stackoverflow.com/questions/31118111/is-in-c-sharp-list-something-like-vector-reserven-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!