Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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#?

like image 691
Dimo Chanev Avatar asked Jun 29 '15 14:06

Dimo Chanev


People also ask

Is an operator in C?

C operators are one of the features in C which has symbols that can be used to perform mathematical, relational, bitwise, conditional, or logical manipulations. The C programming language has a lot of built-in operators to perform various tasks as per the need of the program.

What does %= mean in C?

%= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. C %= A is equivalent to C = C % A.


1 Answers

Use Capacity property:

list.Capacity = n;

or you can set initial capacity via the constructor:

var list = new List<int>(n);
like image 184
Anton Savin Avatar answered Oct 24 '22 11:10

Anton Savin