Sets & Maps

Aris
2 min readFeb 21, 2021

--

Introduction to Sets:

Sets are associative containers that each element has to be unique. You can not modify an element in a set but you can remove it, modify it, and then inserted it.

Some basic functions associated with Sets:

  1. name.begin() → Returns an iterator to the first element in the set
  2. name.end() → Returns an iterator to the next element of the last in the set
  3. name.size() → Returns the number of elements in the set
  4. name.max_size() → Returns the number of elements that a set can hold
  5. name.empty() → Returns whether the set is empty

You can see more functions in the implementation.

Implementation: https://github.com/Aris12345678/Algorithms/blob/main/set.cpp

Introduction to Maps:

Maps are associative containers that store elements in the following way. Each element has a key and a value. Two values can’t have the same key.

Some basic functions associated with Maps:

  1. name.begin() → Returns an iterator to the first element in the map
  2. name.end() → Returns an iterator to the next element of the last in the map
  3. name.size() → Returns the number of elements in the map
  4. name.max_size() → Returns the number of elements that a map can hold
  5. name.empty() → Returns whether the map is empty
  6. name.insert({key, value}) → Inserts a new element in the map
  7. name.erase(iterator position) → Removes the element at the position pointed by the iterator
  8. name.erase(g) → Removes the element with key equal to g
  9. name.clear() → Removes every element in the array

Implementation: https://github.com/Aris12345678/Algorithms/blob/main/map.cpp

Problems:

  1. https://cses.fi/problemset/task/1621 (simple)
  2. https://judge.yosupo.jp/problem/associative_array (simple)
  3. https://cses.fi/problemset/task/1640 (Easy)
  4. http://www.usaco.org/index.php?page=viewproblem2&cpid=667 (Medium)

Reference:

  1. https://www.geeksforgeeks.org/set-in-cpp-stl/
  2. https://www.geeksforgeeks.org/map-associative-containers-the-c-standard-template-library-stl/
  3. https://darrenyao.com/usacobook/cpp.pdf#page=21
  4. https://CPH.pdf#page=47

--

--