Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recitation Outline Hash tables in C++ STL Examples Recursive example

Similar presentations


Presentation on theme: "Recitation Outline Hash tables in C++ STL Examples Recursive example"— Presentation transcript:

1 Recitation Outline Hash tables in C++ STL Examples Recursive example
unordered_set (multiset) unordered_map (multimap) Examples Word puzzle using hash tables Remove duplicate elements from a vector Recursive example Print out numbers

2 unordered_set #include <unordered_set>
using namespace std; std::pair<iterator, bool> insert(const value_type& val); Insert an element val into hash table iterator find(const key_type& k); Search key k void erase(const_iterator position); size_type erase(const key_type& k); Delete an element begin() end() iterators

3 unordered_map #include <unordered_map>
Using namespace std; std::pair<iterator, bool> insert(const value_type& obj); Insert an element typedef std::pair<const Key, T> value_type; void erase(const_iterator position); size_type erase(const key_type& k); Delete an element (with a given iterator or key) iterator find(const key_type& k); Search an element (with given key) begin(), end() Iterators mapped_type& operator[](const key_type& k); Return value if key k exists in map Otherwise an element with key k and default value will be inserted

4 Word Puzzle using hash table
In this version we store word dictionary in an unordered_set See examples/r7/word_puzzle_ht.cpp, word_puzzle_ht.h, rotation.cpp Review the code A demo of running the program

5 Remove Duplicate Elements
Remove duplicate elements from a vector Examples/r7/remove_duplicate1.cpp This version uses a hash table to record unique elements To compile: make remove_duplicate1.x

6 Recursive Function Convert the following function into recursive one
void printnumber(size_t k) { for (size_t I = 0; I <= k; ++i) { cout << I << “ “; } See Examples/r7/printnumber_nonrecursive.cpp And printnumber_recursive.cpp To compile: make printnumber_recursive.x


Download ppt "Recitation Outline Hash tables in C++ STL Examples Recursive example"

Similar presentations


Ads by Google