The three types of containers in C++ — Harold Serrano - Game Engine Developer (2024)

I have to confess, I have grown tired of using the same container type in my game engine. Thus, I opened a C++ book and looked for other type of containers. If you don't know what a container is, a container manages a collection of objects.I have learned that C++ contains three types of containers:

  • Sequential Containers
  • Associative Containers
  • Unordered Containers

Out of the listed above, the sequential container is the type of container you may have used the most. Arrays and vectors belong to this type of container.

Let me teachyou what makes each container type different.

Sequential Containers

These type of containers are ordered-collections in which each element has a position. The position depends on the time and place of insertion. If you append four objects to the end of a vector, these objects will appear in the exact order as you inserted them. The STL provides five sequence container classes:

  • vectors
  • deque
  • arrays
  • lists
  • forward_list

Associative Containers

These aresorted-collectionsin which the position of an elementdepends on its value. The value of the elements determine the position of the elements in the container. The order of insertion doesn't matter. The STL contains four associative container classes:

  • set
  • multiset
  • map
  • multimap

Unordered (associative) Containers

These are unordered-collectionsin which the position of an element doesn't matter. Neither does the order of insertion nor the value of the element. Only their existence in the container matters. If you put four elements in to a collection, their order is undefined and might change over time. The STL container contains four unordered container classes:

  • unordered_set
  • unordered_multiset
  • unordered_map
  • unordered_multimap

The implementation of each type of container differs. For example:

  • Sequence containers are usuallyimplemented as arrays or linked-lists.
  • Associative containers are usuallyimplemented as binary trees.
  • Unordered containers are usually implemented as hash tables.

There you have it. The three types of containers found in the STL are sequential, associative and unordered.

Hope this helps.

PS. Sign up to my newsletter and get development tips.

The three types of containers in C++ — Harold Serrano - Game Engine Developer (1)

Harold Serrano

Computer Graphics Enthusiast. Currently developing a 3D Game Engine.

The three types of containers in C++ — Harold Serrano - Game Engine Developer (2024)
Top Articles
Latest Posts
Article information

Author: Duane Harber

Last Updated:

Views: 6165

Rating: 4 / 5 (71 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.