### roy..'s blog

By roy.., history, 2 months ago,

I know that these habits probably aren't that important at all in terms of improving programming skills. But as I program more often, I find storing items in a vector which is often accompanied by "starting with 0" is handier. Should I make some changes or there's in fact no big difference?

• -6

 » 2 months ago, # |   0 I don't quite understand the question about vector and array. A vector is a dynamic STL array, that is, it allows you to add a new element to the end in O(1) on average. Array is a static structure that does not require additional inclusions. Its main differences from a vector are the impossibility of adding a new element without re-creation and the lack of support for iterators. In most cases, if the size of the array is not fixed and no edge cases are known, programmers use a vector. In sports programming, you can use arrays to avoid unnecessary constants in the asymptotics.About indexing from 1. Most programmers use indexing from 0, it happened historically. You can read Edgar Dijkstra's article "Why numbering should start at zero", where he clearly describes the benefits of such numbering — https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html. In fact, since almost all IT companies use this standard, when applying for a job, you still have to retrain for it :)
•  » » 2 months ago, # ^ |   +2 the lack of support for iteratorsYou always can use pointers as iterators, and there are such helpers as std::begin() and std::end(), which return iterators for arrays too.
•  » » » 2 months ago, # ^ |   0 This is true. However, as far as I remember, std::array is used to work with iterators on static arrays.
•  » » » » 2 months ago, # ^ |   +1 iirc, std::array is actually intended to be a zero-overhead wrapper of the C-style array. That's the main point. It enables a lot of features without extra cost, and being able to use iterators is just one of the many benefits that come along.
•  » » 2 months ago, # ^ |   0 Thank you so much for this convincing response. The question about vectors and arrays derives from top-rated coders' and tutorial preference for vectors over arrays (according to the very limited pieces of code I've seen). So I suggest that vector is a better choice in terms of CP?
•  » » » 2 months ago, # ^ |   0 Quite a contentious issue. You can use either one or the other. Although, for storing a sparse graph, a vector is obviously indispensable. A vector provides more functionality, but don't forget about memory reallocation on resize, which is O(n). Personally, I use the vector much more often. If the written algorithm has the asymptotics implied by the author, then the vector will not affect the sending verdict in any way. I advise you to read the article — https://clck.ru/33QqUK (I hope the translator will help) for more efficient use of this structure.
•  » » » » 2 months ago, # ^ |   0 To avoid reallocation of std::vector from adding elements, you can call the reserve function to allocate enough memory before doing any insertions.
•  » » » » » 2 months ago, # ^ |   0 Yes. This was in the article.
•  » » » » » » 2 months ago, # ^ |   0 oh really? I couldn't load the article (maybe it has to do with the website being Russian or something)
•  » » » » » » » 2 months ago, # ^ |   0 https://drive.google.com/file/d/1pe19ILlSCSkZy_N_giQRM8FMbVYdrI9l/view?usp=sharing here is the file on Google drive.
•  » » 2 months ago, # ^ |   0 "if the size of the array is not fixed and no edge cases are known, programmers use a vector" I didn't understand what you meant by "no edge cases are known" can you give an example?
•  » » » 2 months ago, # ^ |   +1 I assume "edge cases" could be some rare situations that make vectors unusable, that you have to use arrays. For example, since vectors allocate their memory on the heap, it can be slow (usually that doesn't matter that much most of the time).
•  » » » 2 months ago, # ^ |   0 I called the edge cases the maximum size of the array, which is usually written in the task condition. For example, if n (denote by the size of the array) <= 10^5, then it is enough to create a static array of size 10^5: int a[(int)1e5];
 » 2 months ago, # |   0 Use std::vector if you have no obvious reason to use array.