Understanding C++ Standard Library Basics
The C++ standard library is a collection of libraries that is part of every complete C++ implementation.
A Program Built From Nothing
Imagine a program that must read text, store a collection of records, manage memory safely, and run work in parallel. You could try to write every one of those capabilities from scratch, using only the rules of the core language. In practice, almost no serious program is built that way. Real software starts on top of a foundation of existing libraries, and the programmer's own code is the layer added on top of that foundation.
This section introduces the part of that foundation that comes with C++ itself: the standard library. Before you can use it, you need to understand what it is, why it exists, and how it differs from the other systems a real application depends on. Those ideas are the subject of the rest of this article.
Tracing a Task Through the Bare Language
To see why libraries matter, follow one task through two approaches. Suppose the task is a record-keeping program that reads text, keeps a collection of records, handles memory, and does some work in parallel. Each step below is traced first in the bare language and then with the standard library in place.
Tracing the record-keeping task
A program must read text, store a collection of records, manage memory safely, and run work in parallel. How does the effort change when the standard library is available?
- 1State of the task at the start
With only the core language, the programmer has basic building blocks: how to compute, how to make decisions, and how to organize code into functions and types. Those building blocks are small.
- 2Reading text
In the bare language, the programmer must rebuild the machinery for handling text from those small building blocks. The standard library already provides text facilities such as string and the output stream ostream.
- 3Storing a collection of records
Without a library, the programmer writes the storage logic again for this program. The standard library provides vector and map, so the program can name a ready-made collection instead.
- 4Managing memory safely
The standard library supplies
unique_ptrfor this purpose. The programmer names the facility rather than rebuilding the ownership rules by hand. - 5Running work in parallel
The standard library provides thread. Without it, the same parallel machinery would have to be assembled from the core rules alone.
Each capability that would have been rebuilt from scratch is now a named facility. The programmer's effort shifts from reinventing foundations to combining them.
What the Standard Library Is
The C++ standard library is a collection of libraries that is part of every complete C++ implementation.
The standard library is not an optional extra sitting beside the language. It is a large, deliberately designed part of C++. The specification of the standard library takes up almost two thirds of the ISO C++ standard. That proportion shows how much of the language's official definition is devoted to library facilities rather than to core language rules.
Why Libraries Make Programming Efficient
A programming language gives you the basic building blocks: how to compute, how to make decisions, and how to organize code into functions and types. Those building blocks are small. When a task is large, using only them forces you to rebuild the same machinery again and again. The result is code that is tedious to write and easy to get wrong.
Libraries change that equation. Once a set of well-designed facilities exists, the work of a task can often be expressed in a few clear steps that name those facilities. The source puts the contrast plainly: most programs are tedious to write in the bare language, while nearly any task can be made simple with good libraries. Most of the effort moves from building foundations to choosing and combining them.
Libraries are also built in stages. A set of libraries is developed first, and those libraries then form the basis for further work. Each layer is therefore a starting point for the next layer of programs. This is why the standard library is worth learning as a foundation rather than as a list of isolated tools.
The Eight Facilities in the Tour
The standard library offers many facilities. This part of the course gives a quick tour of several of the most useful ones, along with the most common ways of using them. The tour does not require complete mastery of every detail. Its purpose is to build a working sense of which facilities exist and what kinds of tasks they serve.
The facilities introduced in the tour are string, ostream, vector, map, unique_ptr, thread, regex, and complex. They are best understood as siblings: separate tools that share one source of support, the standard library as a whole. The diagram below shows them as direct members of the standard library.
The diagram shows what each facility belongs to, not how each one works. Their individual behavior is covered in later parts of the tour. The way a program reaches any of them is governed by the header and namespace conventions introduced in this section, which come next in the course.
Standard Library Versus Platform Systems
The standard library is not the whole story of a real application. Most implementations also offer graphical user interface systems, web interfaces, and database interfaces. Application-development environments add foundation libraries built for particular corporate or industrial standards. Those systems are useful, but they are not part of the standard library described in this course.
The two columns in the comparison differ in scope. Facilities in the left column are the ones this course treats as the standard library, and they are provided as part of every complete C++ implementation. Entries in the right column may be available in a given environment, but they are separate systems and are outside what this course teaches.
Mistakes Beginners Make With Scope
Common mistake Treating the standard library as an optional extra beside the language
Why it failsThe standard library is a large, deliberately designed part of C++ and is part of every complete C++ implementation.
Do this insteadThink of it as part of C++ itself, and learn it as a foundation.
Common mistake Assuming GUI, web, or database interfaces are part of the standard library
What it looks likeExpecting a window or a database connection to be available just because the program includes standard-library facilities.
Why it failsThose systems are separate from the standard library, even though most implementations also offer them.
Do this insteadCheck whether a capability is a standard-library facility or a platform-specific system before relying on it.
Common mistake Believing every facility must be mastered before any can be used
Why it failsThe tour is meant to build a working sense of which facilities exist and what tasks they serve, without requiring complete mastery of every detail.
Do this insteadLearn the main facilities first, then go deeper into each one as needed.
Predicting Where a Capability Lives
What do you think happens?
A program needs a graphical window and also needs a vector to hold records. Which of these two belongs to the C++ standard library?
Reveal answer
Only the vector belongs to the standard library.
vector is one of the standard-library facilities named in this tour. Graphical user interface systems are platform-specific systems that lie outside the standard library and outside this course's scope.
A team is planning a small program that reads text, keeps a list of customer records, and needs a pattern-matching step to find email addresses. For each part of the plan, decide whether it is covered by a standard-library facility named in this article or whether it falls under a platform or third-party system. Name the facility you would reach for.
Hints
- Look at the eight facilities named in the tour: string, ostream, vector, map,
unique_ptr, thread, regex, and complex. - Pattern matching is the job of one of the named facilities, and the list of records fits a collection facility.
- Anything that asks for a window, a web page, or a database connection is outside the standard library.
Key Takeaways From the Standard Library
- 1The C++ standard library is a collection of libraries that is part of every complete C++ implementation, and its specification takes up almost two thirds of the ISO C++ standard.
- 2Libraries make programming efficient because most tasks are tedious in the bare language but can be made simple with good libraries.
- 3The tour introduces eight facilities: string, ostream, vector, map,
unique_ptr, thread, regex, and complex, all members of the standard library as a whole. - 4GUI, web, and database interfaces, along with industry-specific foundation libraries, are separate from the standard library and outside this course's scope.
- 5The header and namespace conventions that let a program reach these facilities are the next topic to learn.
At a glance
- The C++ standard library is a collection of libraries that is part of every complete C++ implementation.
- Libraries make programming efficient because tasks that are tedious in the bare language become simple when good libraries are available.
- The tour introduces eight facilities: string, ostream, vector, map,
unique_ptr, thread, regex, and complex. - GUI, web, and database interfaces are platform-specific systems separate from the standard library and outside this course's scope.