Containers in C++ Standard Library
Specialized containers are containers that hold elements but have restrictions or added facilities.
The Almost-Containers Question
Many programmers first meet built-in arrays, std::array, and std::string as if they belong to a separate, slightly lesser family of types. They are used constantly, yet they seem to sit on the edge of the container world rather than inside it. This article checks that impression. The goal is to decide whether these types really belong to the container family and, if they do, what makes them different from the containers that fit neatly into the STL framework.
What do you think happens?
Before reading on, decide: does a built-in array count as a container, given that it is often described as almost one?
Reveal answer
Yes, it is a container. The source's test is simple: a container is something that holds elements, and a built-in array holds elements.
The phrase almost containers suggests the type is incomplete. The source rejects that reading. The three specialized types are fully containers in the sense of holding elements. What sets them apart is not that they lack the basic job of storing a collection.
Tracing the Container Test
A specialized container is a container that holds elements but has restrictions or added facilities. The restrictions or facilities are what make it stand apart from the containers that fit the STL framework cleanly.
The source gives one test for whether something is a container: does it hold elements? You can run the test on any candidate by asking two questions. First, is there a collection of items being stored? Second, do those items count as its elements? If both answers are yes, the type is a container, whatever else it does or does not do.
Running the container test on the three source examples
Apply the test (does the type hold elements?) to built-in arrays, std::array, and std::string.
- 1Step 1: Built-in array
A built-in array stores a collection of elements of one element type. The collection is stored in the array, so it holds elements. Result: a container.
- 2Step 2: std::array
std::array stores a collection of elements in the same way. It holds elements, so it passes the test. Result: a container.
- 3Step 3: std::string
std::string stores a sequence of characters, and those characters are its elements. It holds elements, so it passes the test. Result: a container.
- 4Step 4: Compare the results
All three types pass the same test. None of them is excluded because it fails to store a collection. Any difference between them must lie in a restriction or facility, not in whether they are containers at all.
All three source examples hold elements, so all three are containers. They are specialized containers because each carries a restriction or added facility.
Where Specialized Containers Sit in the STL
The standard library provides several containers that do fit the STL framework, along with a smaller group that does not. Both groups are containers. The difference is that the second group carries restrictions or added facilities that make it awkward to treat in the same way as the first group. Separating the two groups keeps the explanation of the STL simpler, because the exceptions can be handled in one place instead of inside every discussion of the framework.
Read the diagram from the top down. Specialized containers are not outside the container family. They sit inside it, in a branch that is set apart for teaching purposes. The three source examples are all in that branch. Both branches belong to the same family, so the specialized branch is a subgroup of containers, not a separate category of things that merely resemble containers.
The Three Source Examples
The source names three examples of specialized containers: built-in arrays, std::array, and std::string. Each one stores a collection of elements. Each one also carries a feature or restriction that sets it apart from the containers that fit the STL framework cleanly. Recognizing these three names is the first practical skill this topic asks for.
| Example | Holds a collection of elements | Sits in the specialized group |
|---|---|---|
| Built-in array | Yes | Yes |
| std::array | Yes | Yes |
| std::string | Yes | Yes |
All three source examples pass the container test and are classed as specialized.
The three examples share the same core fact: each is a container. They differ in the restriction or facility each one carries, and the source does not ask you to memorize those details here. The point of this article is the classification. Once you see the three names as containers, the later question of what each one restricts or adds becomes a question about specialized containers, not about whether they belong to the family at all.
Why Almost Containers Misleads
The phrase almost containers is the impression this article is meant to correct. It implies that the three types are incomplete copies of something else. The source rejects that reading. A container is defined by holding elements, and these types hold elements, so the phrase gets the relationship backwards.
Common mistake Treating a specialized container as outside the container family
What it looks likeReasoning that because std::string is used constantly, it must be a different kind of thing from a container.
Why it failsThe test is whether a type holds elements. std::string holds characters, which are its elements, so it is a container.
Do this insteadClassify the type by the container test first, then ask what restriction or facility it adds.
Common mistake Assuming that a restriction means the type is not a container
What it looks likeConcluding that a built-in array is not a container because it does not behave exactly like the STL containers.
Why it failsA restriction or added facility changes how the container behaves. It does not remove the collection of elements the type holds.
Do this insteadKeep two questions apart: is it a container, and how is it specialized.
Common mistake Describing the STL group as the only real containers and the specialized group as lesser copies
Classifying Types Yourself
A colleague says that std::string is not really a container because it is special. Using the container test from this article, explain why the colleague's reasoning is incomplete and state the correct classification.
Hints
- Start with the question: does the type hold elements?
- Separate the question of whether something is a container from the question of how it is specialized.
- Name the element type that std::string holds.
Explain, in your own words, what the source means when it says specialized containers are containers that hold elements but have restrictions or added facilities. Then say which part of that sentence makes the phrase almost containers misleading.
Hints
- The first clause states what the type is.
- The second clause states how the type differs from the STL group.
Key Takeaways
- A container is something that holds elements, and specialized containers hold elements too.
- Specialized containers are containers that carry restrictions or added facilities, which set them apart from the containers that fit the STL framework.
- Built-in arrays, std::array, and std::string are the source's three examples of specialized containers.
- Calling specialized containers almost containers is misleading, because the phrase implies they are incomplete when they are fully containers.