array vs vector vs list

前端 未结 8 762
日久生厌
日久生厌 2020-11-28 20:34

I am maintaining a fixed-length table of 10 entries. Each item is a structure of like 4 fields. There will be insert, update and delete operations, specified by numeric posi

8条回答
  •  旧巷少年郎
    2020-11-28 20:55

    Use STL vector. It provides an equally rich interface as list and removes the pain of managing memory that arrays require.

    You will have to try very hard to expose the performance cost of operator[] - it usually gets inlined.

    I do not have any number to give you, but I remember reading performance analysis that described how vector was faster than list even for inserts and deletes (under a certain size of course). The truth of the matter is that these processors we use are very fast - and if your vector fits in L2 cache, then it's going to go really really fast. Lists on the other hand have to manage heap objects that will kill your L2.

提交回复
热议问题