When would you use an array rather than a vector/string?

前端 未结 8 1931
醉梦人生
醉梦人生 2020-12-01 19:09

I\'m a beginner C++ programmer and so I\'ve learnt using arrays rather than vectors (this seems to be the general way to do things, then move on to vectors later on).

<
相关标签:
8条回答
  • 2020-12-01 19:48

    When writing code that should used in other projects, in particular if you target special platforms (embedded, game consoles, etc.) where STL might not be present.

    Old projects or projects with special requirements might not want to introduce dependencies on STL libraries. An interface depending on arrays, char* or whatever will be compatible with anything since it's part of the language. STL however is not guaranteed to be present in all build environments.

    0 讨论(0)
  • 2020-12-01 19:49

    I see two reasons:

    1. Compatibility (old code without STL).
    2. Speed. (I compared the speed of using vector/binary_search & array/handwritten binary search. For the last one ugly code was obtained (with reallocation of memory), but it was about 1.2-1.5 times faster then first one, I used MS VC++ 8)
    0 讨论(0)
提交回复
热议问题