Concepts / Containers and Algorithms in the Standard Library

Containers and Algorithms in the Standard Library

The standard library's facilities are classified into groups, which gives you a map of the whole library before you learn each part in detail.

  • Programming

Facing the Whole Library at Once

The C++ standard library is large. It contains facilities for memory allocation, text handling, input and output, data collections, mathematics, concurrency, and more. A programmer who meets this collection for the first time can easily feel overwhelmed, because the individual names do not explain how they relate to one another. Knowing a name such as the one for a container tells you very little about where it fits among the other facilities.

Before reading on, try a small exercise. Imagine you are designing a library that programs could build on. What kinds of help would you expect it to offer? Keep your answer in mind. The categories you are about to see are the standard library's own answer to that question, and comparing your list with theirs is a useful check on your intuition.

Why the Library Is Sorted Into Groups

The standard library solves the overwhelm problem in its own documentation by classifying its facilities into groups. Once you know the groups, a new facility has a place to live in your mental map, and you can predict roughly what it is for before you read its details. The goal of this article is not to memorize every facility. It is to build a picture of the whole library that you can keep returning to as you learn each part in more depth.

The Map of Eleven Groups

The source describes the standard library as a set of facilities that can be classified into eleven groups: run-time language support, the C standard library, strings, regular expression matching, I/O streams, containers and algorithms, numerical computation, concurrent programming, template metaprogramming, smart pointers, and special-purpose containers. Each group answers a different kind of need. Some support the language machinery underneath every program, some support handling text and data, and some support building larger or more general code.

C++ StandardLibraryRun-time languagesupportallocation and typeinformationC standard libraryadapted for C++ type rulesStringsstoring character dataRegular expressionmatchingfinding patterns in textI/O streamsmoving data in and outContainers andalgorithmsorganizing and processingdataNumerical computationmathematical workConcurrentprogrammingwork at the same timeTemplatemetaprogrammingcode across many typesSmart pointersmanaging resourcescorrectlySpecial-purposecontainersarray, bitset, tuple
Which top-level groups does the standard library contain, and what role does each one play?

The diagram shows the eleven groups as siblings under one library. They are not stacked on top of one another. Each is a separate kind of support that the library offers. Read the sublabels as a rough orientation: they name the role each group plays, drawn from what the source says about each one, and the rest of this article explains those roles in turn.

Foundations Under Every Program

The first two categories form the foundation on which the rest of the library rests. Run-time language support provides facilities for allocation and for run-time type information. Allocation is the act of obtaining memory while a program runs. Run-time type information lets a program inspect type information while it executes. These are facilities the language needs in order to operate, which is why they are grouped first. You rarely make them the main topic of a program, but many other facilities depend on them.

The second foundational category is the C standard library. The source states that it is included in the C++ standard library with very minor modifications, and that those modifications exist to minimize violations of the type system. This tells you something about the design. The C++ library did not discard the older C facilities. It adapted them so that they fit the stricter type rules of C++ as closely as possible.

Tracing a Classification Step by Step

What do you think happens?

Which group does memory allocation belong to?

  • Containers and algorithms

  • Run-time language support

  • Special-purpose containers

  • I/O streams

Reveal answer
Answer:

Run-time language support

The source lists allocation, the act of obtaining memory while a program runs, as a facility of run-time language support, the foundation group the language needs in order to operate.

Worked example

Classifying an unfamiliar facility by its purpose

A new facility is described as one that searches character data for patterns written in a special notation. Which group should it be placed in?

  1. 1
    State the purpose

    The facility works on character data and searches it for patterns. Its name is not given, so the purpose is the only clue available.

  2. 2
    Ask whether it supports the language itself

    It does not allocate memory or inspect types at run time, so it is not a foundation group. The question is answered no, and the search moves on.

  3. 3
    Ask whether it handles text or input and output

    Searching character data is text handling. The source places pattern matching in the group for regular expression matching, which sits among the text groups alongside strings and I/O streams.

  4. 4
    Match the purpose to one group

    The purpose matches regular expression matching. The classification came from the purpose, and the name of the facility was never needed.

Result

The facility belongs to the regular expression matching group, one of the text groups, because its purpose is to find patterns inside text.

Asking the Right Questions to Find a Group

The worked example above can be turned into a sequence of questions. Each question checks the purpose of the facility against one family of groups. The flowchart below follows that sequence. A facility leaves the chain at the first question it answers yes, and the order of the questions reflects the order in which the source introduces the groups: foundations first, then text, then data, then advanced concerns, and finally the narrower special-purpose containers.

Supports thelanguage machinery?allocation or typeinformationFoundation groupsrun-time support or ClibraryText groupsstrings, regex, I/O streamsData groupscontainers and algorithms,numericsAdvanced groupsconcurrency,metaprogramming, smartpointersSpecial-purposecontainersdistinct narrow toolsHandles text or I/O?storing, searching, movingcharactersOrganizes data orcomputes?collections or numericalworkConcurrency, genericcode, or resources?advanced concernsNarrower container?array, bitset, or tupleRe-examine thepurposeno question matchedyesnoyesnoyesnoyesnoyesno
Given a new facility, what sequence of questions about its purpose leads to the correct group?

Follow the chain with the example from the previous section. The first question, whether the facility supports the language machinery, gets a no, because regular pattern searching does not allocate memory or inspect types. The second question, whether the facility handles text or I/O, gets a yes, and the chain ends in the text groups. Notice that the chain never asked about the facility's name. Every question was about what the facility does.

Language Support Versus Everyday Tasks

A useful distinction runs across the eleven groups. Some facilities support the language itself: the machinery under every program, such as allocation and run-time type information, and the C standard library that the C++ library adapts. Other facilities support everyday programming tasks: storing and searching text, moving data through input and output, and organizing data in collections. The first kind is rarely the main subject of a program. The second kind is used constantly.

Run-time languagesupportallocation, run-time typeinformationStringscharacter data storageC standard libraryadapted to C++ type rulesI/O streamsinput and outputContainers andalgorithmsorganizing and processingdata
How do facilities that support the language itself differ from facilities that support everyday tasks, side by side?
QuestionLanguage supportEveryday programming tasks
What does it do?Supports the machinery the language needs to operateHelps a program handle text, data, and input and output
Is it usually the main topic of a program?Rarely; many other facilities depend on itOften; these are used constantly
Example groupsRun-time language support, C standard libraryStrings, I/O streams, containers and algorithms

Two families of facilities described in the source

Text, Data, and Advanced Groups in Brief

Many programs spend much of their time with text, so the source groups three related kinds of support together: strings, regular expression matching, and I/O streams. These categories are about how a program stores character data, how patterns are matched inside that data, and how data moves in and out of a program. They are separate because each answers a different question.

The next two categories cover organizing data and computing with it. Containers and algorithms, the STL, and numerical computation are both large and used constantly, which is why they are often the first parts of the library that programmers learn in depth. The last three categories address more advanced concerns: concurrent programming for running work at the same time, template metaprogramming for code that works across many types, and smart pointers for managing resources so that they are handled correctly.

The final group is special-purpose containers. The source lists three: array, bitset, and tuple. All three are containers, but the word special-purpose signals that each serves a narrower role than the general containers of the STL. The source records what each one is for in a separate table, which is the best place to look up the details of a specific member of this group.

Mistakes When Classifying Facilities

  • Common mistake

    Classifying a facility by its name alone

    What it looks like

    Assuming a facility belongs to a group because its name resembles the group's name.

    Why it fails

    The source says to identify purpose and match that purpose to the category, rather than relying on the name.

    Do this instead

    Describe what problem the facility solves, then ask which group handles that kind of problem.

  • Common mistake

    Treating the groups as a stack where one must be learned before the next

    What it looks like

    Believing that a program must first use the foundation groups before it can use containers.

    Why it fails

    The groups are described as separate kinds of support under one library, not as layers stacked on top of one another.

    Do this instead

    Use the map to find where to look. A program may draw on several groups at once.

  • Common mistake

    Assuming a foundation group is the main subject of most programs

    What it looks like

    Expecting to spend most of a project working directly with allocation.

    Why it fails

    The source notes that run-time language support is rarely the main topic, though many other facilities depend on it.

    Do this instead

    Recognize foundation facilities as machinery under the surface, and focus learning effort on the everyday groups.

  • Common mistake

    Confusing the C standard library with a separate library that ignores C++ type rules

Practice: Sorting Facilities Into Groups

MEDIUM

For each description, name the standard library group that fits best. (a) A facility that organizes a collection of values and lets algorithms process them. (b) A facility for running work at the same time. (c) A facility for managing resources so that they are handled correctly. Explain the purpose you matched in each case.

Hints
  • Start by asking what problem each description solves, not what the facility is called.
  • Part (a) describes data organization and computation, a pair of groups the source discusses together.
  • Parts (b) and (c) are two of the three advanced concerns the source lists after the data groups.

Key Takeaways

  • The standard library is classified into eleven groups, which gives a map of the whole library before each part is studied in detail.
  • Foundation groups, run-time language support and the C standard library, support the language machinery, while the other groups support text, data, computation, and advanced concerns.
  • To classify a facility, identify its purpose and match that purpose to a group. Do not rely on the facility's name.
  • A program may use facilities from several groups at once, so the groups tell you where to look rather than which single group to use.

Related concepts

More concepts starting with C