STL Containers Tutorial
Introduction to STL Containers
The Standard Template Library (STL) in C++ provides a set of common classes for data structures and algorithms. STL containers are the collection of objects used to manage collections of data in a structured way. They provide a way to store and manipulate data efficiently and conveniently.
Types of STL Containers
STL containers are broadly categorized into three types:
- Sequence Containers
- Associative Containers
- Unordered Containers
Sequence Containers
Sequence containers store data in a linear fashion. Common sequence containers are:
- vector - Dynamic array
- deque - Double-ended queue
- list - Doubly linked list
Vector
The vector is a dynamic array that provides random access to elements. It can grow and shrink in size dynamically.
Example:
#include <iostream> #include <vector> int main() { std::vector<int> v = {1, 2, 3, 4, 5}; for(int i : v) { std::cout << i << " "; } return 0; }
Deque
Deque (Double-ended queue) is a sequence container that allows insertion and deletion at both ends. It is more efficient than a vector for frequent insertions and deletions at both ends.
Example:
#include <iostream> #include <deque> int main() { std::deque<int> d = {1, 2, 3, 4, 5}; d.push_front(0); d.push_back(6); for(int i : d) { std::cout << i << " "; } return 0; }
List
List is a doubly linked list that allows efficient insertion and deletion at any position. It does not provide random access to elements.
Example:
#include <iostream> #include <list> int main() { std::list<int> lst = {1, 2, 3, 4, 5}; lst.push_front(0); lst.push_back(6); for(int i : lst) { std::cout << i << " "; } return 0; }
Associative Containers
Associative containers store data in a sorted manner and allow fast retrieval using keys. Common associative containers are:
- set - Collection of unique keys
- map - Collection of key-value pairs
- multiset - Collection of keys with duplicates allowed
- multimap - Collection of key-value pairs with duplicates allowed
Set
Set is a collection of unique keys stored in a sorted order. It allows fast retrieval of keys.
Example:
#include <iostream> #include <set> int main() { std::set<int> s = {5, 1, 2, 4, 3}; for(int i : s) { std::cout << i << " "; } return 0; }
Map
Map is a collection of key-value pairs stored in a sorted order based on the keys. It allows fast retrieval of values using keys.
Example:
#include <iostream> #include <map> int main() { std::map<int, std::string> m; m[1] = "one"; m[2] = "two"; m[3] = "three"; for(const auto &pair : m) { std::cout << pair.first << ": " << pair.second << "\n"; } return 0; }
Unordered Containers
Unordered containers store data in an unsorted manner and allow fast retrieval using hash tables. Common unordered containers are:
- unordered_set - Collection of unique keys
- unordered_map - Collection of key-value pairs
- unordered_multiset - Collection of keys with duplicates allowed
- unordered_multimap - Collection of key-value pairs with duplicates allowed
Unordered Set
Unordered set is a collection of unique keys stored in an unsorted manner. It allows fast retrieval of keys using hash tables.
Example:
#include <iostream> #include <unordered_set> int main() { std::unordered_set<int> us = {5, 1, 2, 4, 3}; for(int i : us) { std::cout << i << " "; } return 0; }
Unordered Map
Unordered map is a collection of key-value pairs stored in an unsorted manner. It allows fast retrieval of values using keys and hash tables.
Example:
#include <iostream> #include <unordered_map> int main() { std::unordered_map<int, std::string> um; um[1] = "one"; um[2] = "two"; um[3] = "three"; for(const auto &pair : um) { std::cout << pair.first << ": " << pair.second << "\n"; } return 0; }