C++ STL Introduction
In this page:
#include <container_header>
std::container_name<data_type> variable_name;
std::algorithm_name(variable_name.begin(), variable_name.end());
What is the STL?
The Standard Template Library (STL) is a large set of template classes and functions built into C++ that provides pre-written, highly optimized data structures and algorithms, sparing you from writing basic utilities like sorting or dynamic arrays from scratch.
Example: What is the STL?
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {3, 1, 2};
// Print to the console with cout
std::cout << nums[0] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Containers Overview
Containers are template classes designed to store and manage collections of data, split broadly into sequence containers like vector and deque, which preserve insertion order, and associative containers like set and map, which organize elements by key.
Example: Containers Overview
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <map>
#include <string>
// Program execution starts in main()
int main() {
// Create a vector named sequence (a resizable array)
std::vector<int> sequence = {1, 2, 3};
std::map<std::string, int> lookup = {{"a", 1}};
// Print to the console with cout
std::cout << sequence[0] << " " << lookup["a"] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
STL Algorithms Intro
STL algorithms are free template functions — not member functions of the containers — that operate on ranges of elements to find, sort, copy, or search, which means the same algorithm works uniformly across many different container types.
Example: STL Algorithms Intro
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <algorithm>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {3, 1, 2};
std::sort(nums.begin(), nums.end());
// Print to the console with cout
std::cout << nums[0] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
STL Iterators
Iterators are smart-pointer-like objects used to walk through the elements of a container one at a time, giving every container type — no matter how differently it's implemented internally — a consistent, uniform way to be traversed by algorithms.
Example: STL Iterators
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {1, 2, 3};
// Loop: repeat while the condition holds
for (auto it = nums.begin(); it != nums.end(); ++it) {
// Print to the console with cout
std::cout << *it << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Benefits of the STL
Relying on the STL instead of hand-rolled utilities improves both productivity and reliability: because it's written and maintained by compiler and library specialists, its containers and algorithms are extensively tested, memory-safe, and highly optimized.
Example: Benefits of the STL
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
#include <algorithm>
// Program execution starts in main()
int main() {
// Create a vector named nums (a resizable array)
std::vector<int> nums = {5, 3, 1, 4};
std::sort(nums.begin(), nums.end());
for (int n : nums) std::cout << n << " ";
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using a container without including its header, such as
<vector>or<map>. - Confusing a
vectorwith a fixed-size array. - Using an iterator type that does not match the container.
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: