C++ Standard Library Overview
The C++ Standard Library is a large collection of ready-made classes, functions, and templates that come bundled with every C++ compiler. Instead of writing your own code for common tasks like reading input, storing lists of values, or sorting data, you can use tools the library already provides.
What the Standard Library Includes
You’ve already used pieces of the Standard Library in earlier lessons without necessarily naming it. It’s organized into a few broad areas:
- Input/output —
std::cin,std::cout, and file streams, found in<iostream>and<fstream>. - Strings — the
std::stringclass, in<string>, for working with text. - Containers — data structures like
std::vector,std::map, andstd::array, which store collections of values. - Algorithms — reusable functions such as
std::sort,std::find, andstd::max, found in<algorithm>. - Utilities — helpers like
std::pair, smart pointers, and math functions in<cmath>.
Together these save you from reinventing common building blocks, and they’re well-tested and fast.
Headers and the std Namespace
To use a piece of the library, you #include the header that declares it. For example, <vector> gives you std::vector, and <string> gives you std::string. Including a header only makes the declarations available — it doesn’t pull in unrelated code.
Everything in the Standard Library lives inside the std namespace, which is why you write std::cout or std::vector rather than just cout or vector. The std:: prefix tells the compiler exactly where to find the name, and avoids clashes with names you define yourself.
Example: Combining Several Library Pieces
This program uses four different parts of the Standard Library together: <iostream> for output, <vector> for storing numbers, <algorithm> for sorting them, and <string> for a text label.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
int main(void) {
std::vector<int> numbers = {5, 2, 8, 1, 9};
std::sort(numbers.begin(), numbers.end());
std::string label = "Sorted numbers: ";
std::cout << label;
for (int n : numbers) {
std::cout << n << " ";
}
std::cout << std::endl;
return 0;
}
Output:
Sorted numbers: 1 2 5 8 9
How It Works
The std::vector<int> stores five integers in a resizable list. std::sort rearranges them in ascending order by taking two iterators — numbers.begin() and numbers.end() — that mark the range to sort. A std::string holds the label text, and the range-based for loop walks through the sorted vector to print each value with std::cout.
Notice that none of these tools required you to write sorting logic or a resizable array from scratch — the library already provides correct, efficient versions.
Finding What You Need
You don’t need to memorize the whole library. When you need a new capability, look for the header that matches the task: text handling in <string>, collections in containers like <vector> or <map>, and reusable operations in <algorithm>. As you work through the rest of this course, each of these areas gets its own dedicated lesson with more detail.
Takeaway: the C++ Standard Library gives you tested, ready-to-use tools for I/O, text, collections, and algorithms — include the right header, prefix names with std::, and you’re ready to use them.
