← Back to C++ Course | Chapter 13: STL Containers & Algorithms | Lesson 1 of 15

C++ STL Introduction

The STL is C++'s built-in toolbox of containers, algorithms, and iterators, like a store full of ready-made storage boxes and tools. It saves you writing common things from scratch.
Syntax
cpp
#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?

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using a container without including its header, such as <vector> or <map>.
  2. Confusing a vector with a fixed-size array.
  3. Using an iterator type that does not match the container.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.