The C++ Standard Library is a collection of classes, functions, and templates that provide powerful functionality for I/O, data structures, algorithms, threading, and more. These libraries are included in the std namespace.
Input/Output Library
These libraries handle input and output operations.
| Header | Purpose |
<iostream> | Standard input (cin), output (cout), and error (cerr). |
<fstream> | File handling (ifstream, ofstream, fstream). |
<iomanip> | Formatting output (setprecision, setw). |
<sstream> | String stream (stringstream). |
🔹 Example:
#include <iostream>
#include <fstream>
int main() {
std::cout << "Hello, World!\n"; // Standard output
std::ofstream file("output.txt"); // File output
file << "Writing to a file.";
file.close();
return 0;
}
Container Library
Containers store and manage collections of data efficiently.
| Header | Container Type |
<vector> | Dynamic array (std::vector) |
<array> | Fixed-size array (std::array) |
<list> | Doubly linked list (std::list) |
<deque> | Double-ended queue (std::deque) |
<stack> | Stack (LIFO) (std::stack) |
<queue> | Queue (FIFO) (std::queue) |
<set> | Ordered set (std::set), unordered set (std::unordered_set) |
<map> | Key-value pairs (std::map, std::unordered_map) |
🔹 Example:
#include <vector>
#include <iostream>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
numbers.push_back(6); // Add an element
for (int num : numbers) {
std::cout << num << " ";
}
return 0;
}
Algorithm Library
Provides functions for sorting , searching, and manipulating containers.
| Header | Functionality |
<algorithm> | Sorting (sort), searching (find), min/max (min_element, max_element) |
<numeric> | Numeric operations (accumulate, iota) |
🔹 Example:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> nums = {3, 1, 4, 1, 5};
std::sort(nums.begin(), nums.end()); // Sort the vector
for (int num : nums) {
std::cout << num << " ";
}
return 0;
}
String Library
Provides utilities for working with strings.
| Header | Functionality |
<string> | std::string class for text manipulation |
🔹 Example:
#include <iostream>
#include <string>
int main() {
std::string name = "Alice";
std::cout << "Hello, " + name + "!";
return 0;
}
Multi-Threading Library
Enables concurrent programming.
| Header | Functionality |
<thread> | Create and manage threads |
<mutex> | Synchronization primitives |
🔹 Example:
#include <iostream>
#include <thread>
void hello() {
std::cout << "Hello from thread!\n";
}
int main() {
std::thread t(hello);
t.join(); // Wait for thread to finish
return 0;
}
Random Number Library
Generates random numbers.
| Header | Functionality |
<random> | Random number generation |
🔹 Example:
#include <iostream>
#include <random>
int main() {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dist(1, 100);
std::cout << "Random number: " << dist(gen) << "\n";
return 0;
}
Time and Date Library
Handles time-based operations.
| Header | Functionality |
<chrono> | Time measurement |
🔹 Example:
#include <iostream>
#include <chrono>
#include <thread>
int main() {
using namespace std::chrono_literals;
std::cout << "Waiting...\n";
std::this_thread::sleep_for(2s);
std::cout << "Done!\n";
return 0;
}
Exception Handling Library
Handles errors and exceptions.
| Header | Functionality |
<exception> | Base class for exceptions |
<stdexcept> | Standard exception types (std::runtime_error, std::out_of_range) |
🔹 Example:
#include <iostream>
#include <stdexcept>
int main() {
try {
throw std::runtime_error("An error occurred!");
} catch (const std::exception& e) {
std::cout << "Caught exception: " << e.what() << "\n";
}
return 0;
}