N
N
Nikita Arzamazov2016-07-30 17:49:46
C++ / C#
Nikita Arzamazov, 2016-07-30 17:49:46

Is there an alternative to .Resize() for arrays?

In arrays, you need to declare the size, not like in Lists in c#, for example.
But often Lists are convenient for me, because the number of array elements changes in the process. And in C ++ there are no Lists at all, as far as I know. What to do then? How to correctly implement arrays so that they can be resized, and is there an alternative to .Resize(), because if the number of objects changes frequently, then each time recreating the array, I believe, is resource-intensive. What do you do if you need an array to which you periodically add or remove elements?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Espleth, 2016-07-30
@Arzam

Dynamic array en.cppreference.com/w/cpp/container/vector
In general, they work on the principle "if we have an array of size n and we need to add n + 1 elements, we create a new array of size 2n, copy all the elements there and the new n + 1 th", so you can easily do it yourself.

S
Sergey, 2016-07-30
@sergey_kzn

Use List<>. It is intended for this purpose - to store a changeable set of elements.
C++ has a std::vector that also stores a mutable set of elements.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question