Standard-Library Facilities Tour: vector and map
The C++ standard library is a collection of libraries that is part of every complete C++ implementation.
Building a Program Without Libraries
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 core language rules. 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.
This section introduces the part of that foundation that comes with C++ itself: the standard library. Before we look at any individual facility, we will trace what the library is, why it exists, and how its pieces relate to one another. The two facilities this article focuses on, vector and map, are examples of the containers you will meet as you go deeper.
What do you think happens?
Which standard-library facility would you reach for to hold an ordered list of records, where position matters?
Reveal answer
vector
A vector is the standard-library facility this article uses to represent an ordered, indexed sequence of elements. A map is for pairing keys with values, and string, thread, and the others serve different purposes.
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. Trying to solve a large task with 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. The effort shifts from reinventing foundations to combining them.
The standard library is built in stages. A set of libraries is developed first, and then those libraries form the basis for further work. Each layer is therefore a starting point for the next layer of programs, so a programmer writes the application logic that is unique to the task instead of the machinery that every task shares.
Tracing the Eight Facilities
This part of the course tours eight facilities: string, ostream, vector, map, unique_ptr, thread, regex, and complex. The tour is not meant to make you master every detail. Its purpose is to build a working sense of which facilities exist and what kinds of tasks they serve.
The diagram shows the eight facilities as direct members of the standard library. It does not explain how any one of them behaves. Each is treated in later parts of the tour, and the way a program reaches any of them is governed by the header and namespace conventions that come next in the course.
How a Vector Holds Its Elements
A vector is a contiguous sequence with index-based access. Its elements sit one after another, and you select an element by its position in that sequence. The diagram below uses three placeholder values to show the idea: index 0 is the first element, index 1 the next, and index 2 the last.
When the program writes v[2], it names the element at index 2, which here holds the placeholder value 30. The index is the way to reach the position; the elements themselves stay in their order. This is the shape that makes a vector a natural choice when position in a list matters.
How a Map Pairs Keys with Values
A map is a collection of key-value pairs. Instead of selecting an element by its position, you look up a key and receive the value stored with it. The diagram uses placeholder names and scores to show the pairing.
Reading the diagram from the key side: the key bob is paired with the value 75. A lookup starts from the key and returns its matching value, while the position of the pair is not the point. This key-to-value idea is the reason map appears alongside vector in the tour: one answers where an element sits, the other answers what a named key is associated with.
Choosing a Facility for a Task
Matching a Task to the Right Facility
A program must read a line of text, keep an ordered list of student names in the order they were entered, and look up each student's score by name. Which facilities from the tour fit these needs?
- 1Break the task into needs
The task has three needs: handle text, keep an ordered collection, and find a value from a name.
- 2Match text handling
The string facility from the tour is the one that names text handling, so it fits reading and holding the line of text.
- 3Match the ordered list
The names must stay in entry order and be reached by position, which matches the indexed, contiguous sequence of a vector.
- 4Match the name-to-score lookup
Each name is a key that must return a score, which matches the key-value pairing of a map.
string handles the text, vector keeps the ordered names, and map pairs each name with its score. The three facilities are siblings that work together in one program.
Mistakes When Reading the Standard Library
Common mistake Assuming the standard library is an optional add-on
Why it failsThe source states that the standard library is part of every complete C++ implementation and takes up almost two thirds of the ISO C++ standard.
Do this insteadTreat the standard library as a core part of the language, and expect its facilities to be present wherever C++ is available.
Common mistake Treating vector and map as interchangeable
What it looks likeUsing a position to find a name's score when the data is keyed by name.
Why it failsA vector selects elements by index, while a map selects values by key. They answer different questions.
Do this insteadAsk whether the task needs position in a sequence or association between a key and a value, then choose the matching facility.
Common mistake Expecting the tour to teach every detail of each facility
Why it failsThe tour's purpose is to build a working sense of which facilities exist and what tasks they serve. Each facility is covered in later chapters.
Do this insteadUse this section to recognize the facilities and their roles, then return to later chapters for behavior and details.
Common mistake Confusing the standard library with GUI, web, or database systems
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. These systems are useful, but they are not part of the standard library described in this course.
| Area | Part of the C++ standard library? | Where it lives |
|---|---|---|
| string, vector, map, thread, regex, complex, unique_ptr, ostream | Yes | Standard library covered in this course |
| Graphical user interface systems | No | Platform or third-party systems |
| Web interfaces | No | Platform or third-party systems |
| Database interfaces | No | Platform or third-party systems |
| Corporate or industrial foundation libraries | No | Application-development environments |
Which facilities belong to the standard library and which lie outside this course's scope
Practice Tracing the Tour
A program needs to store a list of product names in entry order and also look up each product's price by its name. For each need, name the facility from the tour that fits, and explain in one sentence why it fits. Then state whether a GUI system would be needed for this task, and why or why not.
Hints
- Ask whether the need is about position in a sequence or about a key that returns a value.
- Think about whether the task involves a window or screen, or only storing and looking up data.
Key Takeaways
- The C++ standard library is a collection of libraries that is part of every complete C++ implementation, and it takes up almost two thirds of the ISO C++ standard.
- Libraries make programming efficient because most tasks are tedious in the bare language but can be made simple with good libraries, and each layer becomes a starting point for the next.
- The eight facilities in the tour are string, ostream, vector, map,
unique_ptr, thread, regex, and complex, and they sit as siblings under the standard library. - A vector selects elements by index along a sequence, while a map selects values by key; choosing between them depends on what the task needs.
- GUI, web, database, and platform-specific foundation systems are useful but lie outside the standard library covered in this course.