Reputation: 2973
I need container like c++ vector. Often it is adviced to use List, but it dosen't support push_back operation. I know this is rather simple implementing an extension method for List container. But. Would Stack be a good alternative?
Thanks!
Upvotes: 2
Views: 18591
Reputation: 1
ListName.Add(element);
has O(1)
complexity
ListName.Insert(index,element);
has O(n)
complexity
as the first one is like the push_back()
function we use in cpp and it will append the element
insert is used mainly when we want to add element at a specific index it involves shifting of element so depending where you put your element complexity may slightly vary as adding element at beginning of large list has to shift rest of the element where as we enter element at the ending of the list it take slightly less time.
Upvotes: 0
Reputation: 15870
You won't get a direct equivalent to vector
in C# because you have no control over how the memory is allocated. std::vector
will always have its memory in a contiguous block; so if needed, you could access it like so:
std::vector<int> v;
// add items to v
CallSomeCFunction(&v[0]);
C# gives you no control over how the memory is allocated, so the distinction between a vector/array and a list is non-existent. You can use the List
container is what you want.
Upvotes: 2
Reputation: 15299
It does support List<T>.Add
. Isn't that what you are looking for?
Upvotes: 20