Standard Containers

The C++ standard libraries include several very useful standard container classes. Containers are just data structures which hold other things, and they are very useful. The structures are array, vector, deque (both array-like structures of variable size), list (which is doubly-linked), set, multiset, map and multipmap. The last four allow fast search based on a key, and they are generally implemented using a tree structure.

The 2011 standard added unordered_set, unordered_multiset, unordered_map and unordered_multimap. These are similar in behavior to the existing structures, but based on hashing rather than a tree.

The array container was also introduced in 2011, which does not mean C++ didn't have arrays before then. The array container is based on the original native array inherited from plain C, which is discussed later.

Associated with each container is an appropriate iterator class which allows a program to step through the contents of the container. An iterator retains a location in a container and is used to loop through the container. If you are familiar with the Interator interface in Java, the purpose is the same, but absolutely everything else about it is different.

Whatever<cont> col; . . . for(Whatever<cont>::iterator i = col.begin(); i != col.end(); ++i) { // Use *i to access the current item. }

It is also possible to use the list form of the for

Whatever<cont> col; . . . for(cont c: col) { // Use c to access the current item. It is a copy of the // item so changing it won't change the contents of col. }

or

Whatever<cont> col; . . . for(cont & c: col) { // Use c to access the current item. It is a reference to the // item, and assigning it will change the contents of col. }

The way you declare cont is much like the way you declare a function parameter. If you want to change the collection, use a reference. If you don't want to change the collection, but the items are large, declare a const reference.

Containers are a C++-only feature.

Textbook: Chapter 10. The book discusses some containers which the notes do not, but does not mention the array container. You will see that behavior between containers of different types is very consistent