Using Standard Libraries in C++
The C++ Standard Library supplies reusable facilities for input and output, character sequences, containers, and algorithms.
Summary
The C++ Standard Library supplies reusable facilities for input and output, character sequences, containers, and algorithms. To use a facility, include its appropriate header, qualify its name where needed, and follow its interface requirements.
| Header | Facilities | Purpose |
|---|---|---|
| <iostream> | std::cin, std::cout | Standard input and output |
| <string> | std::string | Character sequences |
| <vector> | std::vector | Resizable sequences |
The scope-resolution operator in std::cout identifies cout within namespace std. The declaration std::vector<int> values{3, 1, 2}; creates a resizable integer sequence.
Containers manage data, while algorithms operate on data through iterator ranges. Most range-based algorithms use half-open ranges: the starting iterator is included, but the ending iterator is excluded. With <algorithm> included, std::sort(values.begin(), values.end()); sorts the vector in ascending order by default. This algorithm requires random-access iterators.
For std::vector, at() checks bounds and throws std::out_of_range for an invalid index. Understanding iterator validity and function preconditions helps prevent incorrect use, including undefined behavior. Standard interfaces reduce custom implementation work and support source portability across conforming implementations. Container and algorithm choices also affect performance as collections grow.
Common Misconceptions: Including a header does not remove the need for namespace qualification. end() marks the excluded range boundary and must not be dereferenced. std::sort requires random-access iterators, not just any iterator type.
🧠 Key Concepts
- Standard headers
- Namespace qualification
- Standard streams
- Resizable sequences
- Half-open ranges
- Random-access iterators
- Bounds checking
- Interface preconditions
🧠 Quick Check
See what you remember from the summary.
A program uses std::cin and std::cout. Which header declares these stream objects?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Using Standard Libraries in C++
📘 Overview The C++ Standard Library provides reusable facilities for input and output, strings, containers, and algorithms. Programs access these facilities through standard headers and names usually qualified with std::.
🧠 Key Idea Include the appropriate header, select a library facility that matches the task, and follow its interface requirements. Containers manage data, while algorithms operate on data through iterator ranges.
⚔️ Core Details: - Headers declare library facilities: <iostream> provides standard streams, <string> provides std::string, and <vector> provides std::vector. - The scope-resolution operator qualifies library names: std::cout identifies cout in namespace std. - std::vector<int> values{3, 1, 2}; creates a resizable sequence of integers. - Most range-based algorithms use a half-open iterator range: the first iterator is included and the last iterator is excluded. - With <algorithm> included, std::sort(values.begin(), values.end()); sorts a vector in ascending order by default. - For std::vector, at() checks bounds and throws std::out_of_range for an invalid index.
🎯 Why It Matters: - Standard facilities reduce custom implementation work and make program intent easier to recognize. - Standardized interfaces support source portability across conforming C++ implementations. - Understanding iterator validity and function preconditions prevents incorrect library use, including undefined behavior. - Container and algorithm choices affect performance as programs process larger collections.
🧠 Quick Recall: - std:: - Qualifies a name within the standard-library namespace. - std::cin and std::cout - Standard input and output stream objects declared in <iostream>. - std::string - A character-sequence class declared in <string>. - Half-open range - Includes the starting iterator and excludes the ending iterator; end() is not dereferenceable. - std::sort - An algorithm declared in <algorithm> that requires random-access iterators.
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Engineering notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.