Adding elements to an array | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Adding elements to an array

The description of this topic is in an answer.

26th Aug 2016, 7:50 PM
Néstor
Néstor - avatar
3 Answers
+ 1
Description answer: First of all, adding values to an array is very very useful. For example, if i want to make a program about prime numbers, storing the prime numbers that i'm finding, the only thing i've to do is add this numbers to my list. The truth is that in python3, this is very easy to do (at the expense of performance). The way to do that on C++ is using <vector> header (vector class, that's like a modifiable array). The method vector.push_back(value) adds the value at the end of the array. There are methods to delete and change the content of a vector, so i will be glad if someone is being crazy like me with this topic on C++ and then see that.
26th Aug 2016, 7:54 PM
Néstor
Néstor - avatar
0
The class vector also supports element access via reference, in a bounds checked (expensive) way via at method and a unchecked element access via [] operator. @Néstor: While python is slower than C++, the abstract data structure "self-sizing container" implemented is still pretty much the same. Apart from the usual overhead of python, there should be not much of a difference. (The strategies how to best increase the size of the underlying array (the most expensive operation) has been researched and implemented quite a long time ago).
26th Aug 2016, 10:29 PM
Stefan
Stefan - avatar
0
Great point Stefan, thanks. Anyway, C++ is still much efficient in general and for me using a expensive operation in an efficient language is good enough in comparison with a continuous unefficient but friendly language.
26th Aug 2016, 10:40 PM
Néstor
Néstor - avatar