In which scenario do I use a particular STL container?

后端 未结 10 928
遥遥无期
遥遥无期 2020-11-22 00:31

I\'ve been reading up on STL containers in my book on C++, specifically the section on the STL and its containers. Now I do understand each and every one of them have their

10条回答
  •  花落未央
    2020-11-22 01:21

    An important point only briefly mentioned so far, is that if you require contiguous memory (like a C array gives), then you can only use vector, array, or string.

    Use array if the size is known at compile time.

    Use string if you only need to work with character types and need a string, not just a general-purpose container.

    Use vector in all other cases (vector should be the default choice of container in most cases anyway).

    With all three of these you can use the data() member function to get a pointer to the first element of the container.

提交回复
热议问题