Thư viện STL C++

STL (Standard Template Libaray) là bộ các template class cung cấp các class và function được dùng để implement cấu trúc dữ liệu và thuật toán. STL gồm 3 pattern:

  • Container
  • Algothirm
  • Iterator

STL cung cấp nhiều container và các thuật toán rất hữu ích trong việc lập trình, ví dụ bạn có thể dễ dàng define link list chỉ bằng một câu lệnh, bằng cách sử dụng link list của thư viện STL, tiết kiệm thời gian và công sức của bạn.

Containers

Container chia ra làm 3 loại:

Sequence Containers
Sequence Container implement các cấu trúc được truy cập tuần tự

array : mảng tĩnh lưu các giá trị, C++ 11 trở lên
vector: mảng cấp phát động
deque: Double End Queue
forward_list: danh sách liên kết đơn
list: danh sách liên kết đôi.


Associative containers

Associative containers implement các cấu trúc dữ liệu đã được sắp xếp, để tối ưu việc tìm kiếm.

set : collection of unique keys, sorted by keys
map : collection of key-value pairs, sorted by keys, keys are unique
multiset : collection of keys, sorted by keys
multimap : collection of key-value pairs, sorted by keys

Unordered associative containers
Unordered associative containers implement cấu trúc dữ liệu chưa được sắp xếp, để tối ưu việc tìm kiếm

unordered_set: collection of unique keys, hashed by keys
unordered_map: (since C++11) collection of key-value pairs, hashed by keys, keys are unique
unordered_multiset : (since C++11) collection of keys, hashed by keys
unordered_multimap : (since C++11) collection of key-value pairs, hashed by keys

Container adaptors
Container adaptors cung cấp 1 interface khác cho sequence container.

stack : adapts a container to provide stack (LIFO data structure)
queue : adapts a container to provide queue (FIFO data structure)
priority_queue : adapts a container to provide priority queue.