Concepts / Standard-Library Components

Standard-Library Components

The C++ standard library is a set of libraries that forms the foundation for most serious programming work, since good libraries make tasks far simpler than the bare language does.

  • Programming

Why Programs Lean on Libraries

Imagine writing a program using only the rules of the core language, with no prebuilt pieces to draw on. Every task that involves text, groups of values, output, or running work at the same time would have to be built from the ground up. Experienced C++ programmers almost never work this way. Instead, a body of libraries is developed first, and that body becomes the foundation for everything built afterward.

The difference in effort is large. Most programs are tedious to write in the bare language, while good libraries make nearly any task simple. The C++ standard library is the collection of libraries that forms the foundation for most serious programming work. This article follows one idea through the whole tour: a task is easier when you can match each of its steps to a facility someone has already built and specified for you.

Where the Standard Library Ends

The standard library is not a small add-on. Its specification takes up almost two thirds of the ISO C++ standard, which gives a sense of how much of the language definition is devoted to it. Every complete C++ implementation includes the standard-library facilities described in this tour. You can therefore rely on them wherever a conforming implementation is available.

C++ programStandard libraryfacilitiesPresent in everycompleteimplementationPlatform-specificsystemsGUI systemsWeb interfacesDatabase interfacesCorporate foundationlibrariesrelies onmay useguaranteed byincludesincludesincludesincludes
Which parts of a C++ program come from the standard library, and which come from platform-specific systems that lie outside it?

The diagram makes one boundary easy to see. On one side are the standard facilities, which belong to every complete implementation. On the other side are graphical user interface systems, web interfaces, and database interfaces. Most implementations offer these too, and most application-development environments provide foundation libraries for their own corporate or industrial use. Those interfaces are real and useful, but they are supplied by particular systems and environments, so they are not part of the standard facilities this tour introduces.

Specification Versus Implementations

Definition

The standard defines what the library must provide. Implementations are the concrete versions that supply those facilities on real systems. The standard is the specification; an implementation is one working realization of it.

ISO C++ standardStandard libraryspecificationImplementation oneYour programImplementation twoImplementation threecontainsrealized byrealized byrealized bydepends on the specified behavior
How does one standard library specification relate to the implementations that supply it, and what does a program depend on?

The diagram shows why the distinction matters. A program is written against the specification, and it can run on any conforming implementation that supplies the facilities it uses. The same code is meant to work across those implementations. This is the reason the tour's examples are written to be portable: they aim to describe C++ as the standard defines it, not as one particular system happens to provide it.

The Eight Named Facilities

This tour introduces eight named standard-library types: string, ostream, vector, map, unique_ptr, thread, regex, and complex. Each one stands for a family of facilities that the standard library provides. The goal at this stage is recognition rather than full mastery. You are learning which facility handles which kind of job, not how to use every member of each family.

Standard libraryTextstringGroups of valuesvectorOutputmapRunning work at thesame timeostreamNamed in the tour,roles not describedin the sourcethreadunique_ptrregexcomplexcoverscoverscoverscoverscoversexampleexampleexampleexampleexampleintroducedintroducedintroduced
How are the eight facilities grouped, and what is each one responsible for relative to the others?

The first four groupings follow the kinds of work the source names directly: text, groups of values, output, and running work at the same time. string handles text, vector and map handle groups of values, ostream handles output, and thread handles work that runs at the same time. The source does not state what unique_ptr, regex, or complex are for, so this article does not assign them a job. The tour introduces them by name, and their details belong to later, more detailed parts of the course.

Tracing a Task Through the Facilities

The tour suggests a way of thinking: start with a plain task, identify the kind of data involved, and then ask which named facility fits each step. Watch how the program's state grows as you move through the steps. At the start there is nothing stored, and each step adds one more piece of information the program has to keep track of.

What do you think happens?

At the very start of a word-count task, before any step has run, how much information does the program have to keep track of?

  • Everything in the input text

  • Nothing stored yet

  • One word

Reveal answer
Answer:

Nothing stored yet

At the start nothing is stored. Each step then adds one more piece of information the program has to keep track of.

Worked example

Counting how often each word appears

Given a plain text, report how many times each word appears, then print the results. The mapping of steps to facilities below is an illustrative exercise, not a statement from the source about how the facilities are used internally.

  1. 1
    Start with nothing stored

    Before the task begins, the program holds no words, no groups, and no counts. The state is empty.

  2. 2
    Hold the input as text

    The input is text, so the first facility that fits is string. After this step the program holds one piece of information: the text itself.

  3. 3
    Group the words together

    The words form a group of values in sequence, so vector is the facility to consider. The state now holds the text and a group of words.

  4. 4
    Count each distinct word

    Each word needs a count attached to it, which is a keyed association of a word with a number. map fits this job. The state grows again, now with one count for each distinct word.

  5. 5
    Print the results

    The results are output, so ostream fits this step. Nothing new has to be kept, and the task is finished.

Result

The task is handled by four facilities working together: string holds the text, vector groups the words, map keeps the counts, and ostream prints them. At each step the program's state grew by exactly one more piece of information.

Notice the pattern. You did not write any of the four facilities. You identified the kind of data at each step and matched it to a named facility. The programmer's work moved from building storage and output logic from scratch to deciding which facility fits each part of the task.

Standard Facilities Versus Home-Made Code

The standard defines what the library must provide, and much thought went into its design. Much more effort went into its implementations, and much more will continue to go into its maintenance and extension. For that reason, the advice is to explore the standard library and to prefer it over home-made alternatives.

AspectStandard facilityHome-made alternative
DefinitionSpecified by the standard, which says what the library must provideDefined only by whoever wrote it
AvailabilityPart of every complete C++ implementationMust be written, shipped, and kept working by the programmer
PortabilityExamples are meant to work across conforming implementationsPortability depends on the programmer's own care
Design and upkeepShaped by a large body of design work, with ongoing maintenance and extensionUpkeep falls to the programmer alone
ScopeCovers text, groups of values, output, concurrent work, and the other named facilitiesCovers only what the programmer chose to build

Standard facilities compared with home-made equivalents, based on the points the source makes about specification, portability, and maintenance.

Mistakes When Reading the Library Boundary

  • Common mistake

    Treating the standard library as a small add-on to the language

    Why it fails

    The standard library's specification takes up almost two thirds of the ISO C++ standard, so it is a central part of the definition.

    Do this instead

    Think of the standard library as a core part of C++ that every complete implementation includes.

  • Common mistake

    Assuming GUI, web, or database interfaces are part of the standard library

    Why it fails

    Those systems are supplied by particular systems and environments and are separate from the standard facilities.

    Do this instead

    Check whether a facility belongs to the standard set. If it is a GUI, web, or database interface, it is outside this part of the course.

  • Common mistake

    Writing a home-made equivalent of a standard facility by default

    Why it fails

    The standard facilities are specified, included in every complete implementation, and maintained over time, and the advice is to prefer them.

    Do this instead

    Explore the standard facility first, and build your own only when you have a clear reason.

  • Common mistake

    Expecting the tour to explain how each facility stores its data

    Why it fails

    The tour aims to show which facility handles which job, and it explicitly does not explain internal storage.

    Do this instead

    Learn the job each facility serves now, and leave internal details for the later, more detailed parts of the course.

Practice Matching Jobs to Facilities

EASY

Choose a plain task of your own, such as reading a list of names and printing them in sorted order. Write down the kind of data at each step. For each step, name the standard facility from the tour that fits best, and state how many pieces of information the program is keeping track of after that step. Use only the eight named facilities in your answer, and say so if a step does not clearly fit any of them.

Hints
  • Start by listing the steps in order and writing down the state before the first step, which should be nothing stored.
  • Ask whether each step deals with text, a group of values, output, or work running at the same time.
  • If a step does not clearly match one of the eight named facilities, state that rather than guessing a facility for it.

Key Takeaways

  • Real C++ programs rely on prebuilt standard-library facilities, because building text handling, groups of values, output, and concurrent work from the bare language is tedious.
  • The standard library's specification takes up almost two thirds of the ISO C++ standard, and every complete implementation includes its facilities.
  • The standard defines what the library must provide, while implementations are the concrete versions that supply those facilities; the standard version is preferred over home-made alternatives.
  • The tour names eight facilities: string, ostream, vector, map, unique_ptr, thread, regex, and complex. Its goal is recognition, and it does not explain their internal storage.
  • GUI, web, and database interfaces are platform-specific systems that lie outside the standard library and are not covered in this part of the course.

Related concepts

More concepts starting with S