Part 3 - stdlib containers
In this part I'll quickly show a few of really useful containers available in stdlib.
common stuff
- Avoid using
[]
operator asat
method does the same, but provides error handling. - If you plan to create a new object and immediately insert it into container, use
emplace
instead ofpush
/insert
. - Using stdlib containers allows you to use a lot of goodies from algorithms library.
- If you really need to use container's pathetic C form (like raw pointers to data) you always can, by using
.data()
(but at what cost). - Stop using glib as it's pure cancer
arrays/lists
std::vector
- dynamic length array, bread and butterstd::array
- static length array, replacement forint[]
std::deque
- double ended queuestd::forward_list
- singly-linked liststd::list
- doubly-linked list
// create vector with some data
auto a = std::vector<int>{1, 2, 3, 4};
// inserting elements
a.push_back(5);
// emplacing elements
a.emplace_back(5);
// for each loop
for (const auto& elem : a)
std::cout << elem;
// check element at index
std::cout << a.at(2);
auto b = std::array<int>{1, 2, 3, 4};
std::sort(b.begin(), b.end());
adaptors
These components adapt API to containers from previous point.
std::stack
- LIFOstd::queue
- FIFOstd::priority_queue
- you can guess
// TODO: provide examples
quickly searchable lists
std::set
- sorted list of unique keysstd::unordered_set
- unorderedstd::set
std::multiset
-std::set
with duplicatesstd::unordered_multiset
- unorderedstd::multiset
// TODO: provide examples
key-value pairs (maps/dicts)
std::map
- sorted mapstd::unordered_map
- unsortedstd::map
// TODO: provide examples