Concepts / Overview of Standard Library Headers

Overview of Standard Library Headers

A Standard Library header is a header file that supplies declarations for a particular kind of functionality, such as containers, algorithms, or utilities.

  • Programming

Finding a Name Before You Can Use It

Every C++ program that uses the Standard Library depends on named tools: sorting a list, storing key-value pairs, or reading a file all rely on entities such as sort(), map, or ifstream. The Standard Library does not put all of these into one place. It spreads them across a collection of header files, each grouped by purpose. Knowing which header supplies which entity is therefore the first practical skill for working with the library.

What do you think happens?

Before reading on: when you write vector in a program that uses the Standard Library, what namespace does its real name belong to?

  • The global namespace, with no prefix

  • A namespace called vector

  • namespace std

Reveal answer
Answer:

namespace std

Every header in the selected listing supplies its declarations in namespace std, which is why the library name is written as std::vector. The header is where you include it from, and std is where the name lives.

What a Standard Library Header Supplies

Definition

A Standard Library header is a header file that supplies declarations for a particular kind of functionality, such as containers, algorithms, or utilities.

A header is therefore a grouping by purpose, not a single tool. Inside it are declarations, which are the names and interfaces of the classes and functions the header offers. A declaration tells the compiler that a name exists and how it may be used. When you need a feature, you look up the header that carries its declaration and include that header in your source file. The header does not stand alone: it is one entry in a layout of many headers, and each one covers one functionality group.

Namespace std as the Home of Declarations

Every header in the selected listing supplies its declarations in namespace std. A namespace is a named scope that groups related names, and std is the namespace where the Standard Library defines its declarations. This is the reason library names are written with the std:: prefix, as in std::copy(), std::find(), and std::sort(). The prefix is not decoration. It states which scope the name belongs to.

namespace stdscope for librarydeclarationsvectorcontainersort()algorithmmove()utilitypairutilitydeclaresdeclaresdeclaresdeclares
Which scope holds the declarations of vector, sort(), move(), and pair?

The diagram reads as two relationships. A header supplies declarations, and those declarations belong to std. The header is the place you include; the namespace is the place the name lives. Keeping these two ideas separate prevents the most common confusion about the library, which is covered in the mistakes section below.

cpp
Notes on the code
  • The include line brings in the header that carries the declaration.
  • std:: names the namespace where vector is declared.
  • The unqualified name vector is not used here; the prefix is required.

Tracing a Name from Header to Use

Work through the lookup process one step at a time. Each step changes what the compiler knows about the name. Follow the trace for a container, which is the first family in the listing.

  1. 1
    Find the functionality group the name belongs to. vector is a container.
  2. 2
    Look up the header row in the listing that pairs a header with the container declarations it supplies.
  3. 3
    Write the include line for that header at the top of the source file so its declarations are available.
  4. 4
    Refer to the name with the std:: prefix, because the declaration lives in namespace std.
  5. 5
    Use the name in code. Without the include, the declaration is not available to the source file.
Worked example

Naming a container from the library

A program needs a growable list of integers. Identify the library entity that fits, state the namespace it belongs to, and write the name the program must use.

  1. 1
    Classify the need

    A growable list of values is a container, the first of the three families in the listing.

  2. 2
    Identify the entity

    vector is the container named in the listing for this purpose.

  3. 3
    Locate the namespace

    Every header in the listing supplies its declarations in namespace std, so vector belongs to std.

  4. 4
    Write the qualified name

    The name in the program is std::vector<int>, with the header included at the top of the file.

Result

std::vector<int>, declared in namespace std and supplied through the header that the listing pairs with containers.

Reading the Header Listing

The selected listing has two columns that matter for this lesson: the header name and the declarations it supplies. Each row pairs one header with one or more named entities. Where the original listing wrapped a header or entity name across lines, the names appear here in complete form. For example, the algorithm sort() and the memory header <memory> each appear whole.

FamilyEntities named in the listingUse it when you need
Containersarray, vector, forward_list, map, set, and the unordered variantsto store and organize a collection of values
Algorithmssort(), find(), copy()to operate on a range of values
Utilitiespair, move(), swap()to combine values, move them, or exchange them
Other headersinput and output, strings, regular expressions, time, random numbers, threads, file streamsto handle the matching area of work

Three families that recur through the listing, plus other groups. The grouping is a reading aid, not a formal category system stated by the source.

Header filegrouped by purposeDeclarationsnames and interfacesnamespace stdwhere the names livesuppliesbelong to
How does one header file relate to the declarations it supplies, and where do those declarations live?

Mistakes Beginners Make with Headers

  • Common mistake

    Treating the header as the namespace

    What it looks like

    Thinking that including a header is the same as naming std, so writing vector<int> without any prefix.

    Why it fails

    The header is the place you include from, while the namespace is the place the name lives. The declaration sits in std whatever header carries it.

    Do this instead

    Qualify library names with std::, as in std::vector<int>.

  • Common mistake

    Assuming the listing is complete

    What it looks like

    Searching the listing for a name, failing to find it, and concluding the library has no such feature.

    Why it fails

    The listing is far from complete and works as a starting map, not a full reference.

    Do this instead

    Use the listing to learn the layout, and consult a complete reference when a name is missing.

  • Common mistake

    Using a grouping label as a formal rule

    What it looks like

    Believing that a name in the Algorithms family must always come from one specific header because it was grouped there.

    Why it fails

    The three families are a reading aid rather than a formal category system stated by the source.

    Do this instead

    Treat the families as a way to scan the listing, and rely on the header row for the exact pairing.

  • Common mistake

    Forgetting the include line while still writing the std:: prefix, so the name is not declared in the source file.

Practice Finding a Header

EASY

Using the three families in the listing, state the family for each of these names: vector, sort(), and swap(). Then write the qualified name you would use for sort() in a program. Check your answer against the table in the reading section.

Hints
  • Compare each name against the entities listed under Containers, Algorithms, and Utilities.
  • Remember that library names carry the std:: prefix.

Key Takeaways

  • A Standard Library header is a header file that supplies declarations for one kind of functionality, such as containers, algorithms, or utilities.
  • Every header in the selected listing supplies its declarations in namespace std, so library names are written with the std:: prefix.
  • The header is the place you include from, and the namespace is the place the name lives. Keep these two ideas separate.
  • Use the listing to look up which header carries a given container, algorithm, or utility, remembering that it is a starting map and not a full reference.

Related concepts

More concepts starting with O