76. Use vector by default. Otherwise, choose an appropriate container

Summary

Using the “right container” is great: If you have a good reason to use a specific container type, use that container type knowing that you did the right thing.

So is using vector: Otherwise, write vector and keep going without breaking stride, also knowing you did the right thing.

Discussion

Here are three fundamental issues and related questions for programming in general, and for choosing a container in particular:

Write for correctness, simplicity, and clarity first (see Item 6):Prefer to choose the container that will let you write the clearest code. Examples: If you need to insert at a specific position, use a sequence container (e.g., vector, list). If you ...

Get C++ Coding Standards: 101 Rules, Guidelines, and Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.