From 4f1d1f27e7f576899202fbfdbc7d353adea31c2f Mon Sep 17 00:00:00 2001 From: Morten Nobel-Joergensen Date: Wed, 25 Jul 2018 15:43:56 +0200 Subject: [PATCH] Add reverse, unordered_set, set, unordered_map --- README.md | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c5f5edc..5c640db 100644 --- a/README.md +++ b/README.md @@ -372,7 +372,6 @@ getline(cin, s); // Read line ending in '\n' ## `vector` (Variable sized array/stack with built in memory allocation) - ```cpp #include // Include vector (std namespace) vector a(10); // a[0]..a[9] are int (default size is 0) @@ -412,7 +411,7 @@ a.first; // "hello" a.second; // 3 ``` -## `map` (associative array - usually implemented as red-black trees) +## `map` (associative array - usually implemented as binary search trees) ```cpp #include // Include map (std namespace) @@ -423,6 +422,39 @@ for (auto& p:a) a.size(); // 1 ``` +## `unordered_map` (associative array - usually implemented as hash table) + +```cpp +#include // Include map (std namespace) +unordered_map a; // Map from string to int +a["hello"] = 3; // Add or replace element a["hello"] +for (auto& p:a) + cout << p.first << p.second; // Prints hello, 3 +a.size(); // 1 +``` + +## `set` (store unique elements - usually implemented as binary search trees) + +```cpp +#include // Include set (std namespace) +set s; // Set of integers +s.insert(123); // Add element to set +if (s.find(123) != s.end()) // Search for an element + s.erase(123); +cout << s.size(); // Number of elements in set +``` + +## `unordered_set` (store unique elements - usually implemented as a hash set) + +```cpp +#include // Include set (std namespace) +unordered_set s; // Set of integers +s.insert(123); // Add element to set +if (s.find(123) != s.end()) // Search for an element + s.erase(123); +cout << s.size(); // Number of elements in set +``` + ## `algorithm` (A collection of 60 algorithms on sequences with iterators) ```cpp @@ -431,4 +463,5 @@ min(x, y); max(x, y); // Smaller/larger of x, y (any type defining <) swap(x, y); // Exchange values of variables x and y sort(a, a+n); // Sort array a[0]..a[n-1] by < sort(a.begin(), a.end()); // Sort vector or deque +reverse(a.begin(), a.end()); // Reverse vector or deque ```