Standard-Library Facilities Tour: string and ostream
The C++ standard library is a collection of libraries that is part of every complete C++ implementation.
Building a Program 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.
What do you think happens?
If a program needs to store a collection of records, read text, and run work in parallel, what do you think most programmers rely on: rebuilding each capability from the core language rules, or building on existing libraries?
Reveal answer
Build on existing libraries and write only the layer that is specific to the task.
The source states that almost no serious program is built by rebuilding everything from the core rules. Real software begins with a foundation of existing libraries, and the programmer's code is added on top of it.
This article follows one thread through that foundation. It starts with the part that comes with C++ itself, the standard library, and then traces how two of its facilities, string and ostream, work together to put text on the screen. Along the way you will see where the standard library ends and other kinds of systems begin.
Tracing a Task Written in the Bare Language
A programming language gives you its 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 you try to solve a large task with only them, you are forced to rebuild the same machinery again and again. Each time you need a growing collection of items, a way to hold text, or a way to share work across processors, you write the low-level logic yourself.
- 1Start with a task that needs text, a collection of records, and safe memory handling.
- 2Write the low-level logic for each capability using only the core language rules.
- 3Notice that the same machinery is being rebuilt for every new task.
- 4Observe the result: the code becomes 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. The 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.
Two things are true at once. The core language rules define what the language can express, and the standard library supplies ready-made facilities built on those rules. A complete implementation of C++ includes the standard library, so a programmer who has a complete implementation already has these facilities available. The diagram below shows how the standard, the language rules, the implementation, and the library facilities relate to one another.
Tracing the Eight Facilities as Siblings
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.
- string: text handling
- ostream: output streams
- vector: collections of items
- map: keyed lookup of values
unique_ptr: safe ownership of memory- thread: running work in parallel
- regex: pattern matching on text
- complex: complex numbers
These eight facilities are siblings. Each is a separate tool, and all of them share one source of support: the standard library as a whole. Their individual behavior is not explained here. Each one is treated in later chapters of the tour, and the way a program reaches any of them is governed by the header and namespace conventions introduced in this part of the course.
The 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 of the standard library is therefore a starting point for the next layer of programs, which is why a single facility such as string can be used as a building block in many different programs.
Following Text Into the Console
Two facilities in the tour, string and ostream, cooperate on a very ordinary job: printing text. A string holds a piece of text, and an output stream is the facility that carries values out of the program. The following fragment is an illustrative example written for this article, not a complete program. It shows the statement a learner will see first, and the parts of it will be examined in the next section.
Ada- std::string names a standard-library text facility and holds the value
- std::cout is the output stream facility that writes to the console
- << is the operator that sends the value from name into the stream
- The whole statement ends with a semicolon
Read the data flow from left to right. The string value named name is passed through the << operator into the output stream, and the stream carries that text out to the console. The diagram below traces that path step by step.
Reading One Output Statement Piece by Piece
Each part of std::cout << name; contributes something different. The std:: prefix is a namespace convention: it tells the reader that cout belongs to the standard library's namespace. The name std::cout identifies a stream object, a facility whose job is to receive output. The << operator is the action that sends the value on its right into the stream on its left. The name is the value being sent. The semicolon ends the statement.
Separating the Standard Library From 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. Keeping the two apart prevents a common confusion about what a program can rely on in every complete C++ implementation.
Mistakes Learners Make With the Standard Library Boundary
Common mistake Assuming every system a program uses is part of the standard library
What it looks likeTreating a GUI window toolkit or a database interface as just another standard facility.
Why it failsThe source states that GUI, web, and database interfaces are separate from the standard library.
Do this insteadSort each facility by asking whether it is a standard-library component or a platform or third-party system.
Common mistake Believing the standard library is an optional add-on to the language
What it looks likeSkipping the library because the core rules seem sufficient for a small task.
Why it failsThe standard library is part of every complete C++ implementation and takes up almost two thirds of the ISO standard.
Do this insteadTreat the standard library as a designed part of C++ that is available with a complete implementation.
Common mistake Expecting to master every detail of string and ostream before using them. The tour is meant to build a working sense of which facilities exist.
Practicing the Boundary Between Facilities
For each item below, decide whether it is a C++ standard-library facility named in this tour or a system outside the standard library: string, a web interface, vector, a database interface, ostream, a GUI system, unique_ptr. Then explain in one sentence what the standard library gives a programmer that the bare language alone does not.
Hints
- The eight facilities named in this tour are string, ostream, vector, map,
unique_ptr, thread, regex, and complex. - GUI, web, and database interfaces are named in the source as separate from the standard library.
- Think about the difference between rebuilding machinery from the core rules and combining ready-made facilities.
Key Takeaways
- The 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.
- Libraries make programming efficient because tasks that are tedious in the bare language become simple when good libraries provide the foundation.
- The tour's eight facilities, including string and ostream, are siblings that share the standard library as their common source of support.
- In a statement such as std::cout << name;, the std:: prefix is a namespace convention, std::cout is an output stream, << sends the value into it, and the value flows to the console.
- GUI, web, database, and industry-specific foundation systems are useful but lie outside the standard library covered in this course.