Skip to content

Module 4 : STL Containers

Bayu Adjie Sidharta edited this page May 19, 2021 · 8 revisions

C ++ Standard Template Library Container

Container is an object that is used to store a collection of other objects (called elements in the container) and manage their storage space. In C ++, container is implemented as a template class, so that there are functions to access its elements.

std::array

Do you still remember Array? std::array is a type of data structure that holds elements sequentially with a fixed size (fixed-size). Operations on std::array:

  • operator [] - Accesses the element at a specific position.
  • at() - Accesses the element at a specific position while checking the array size limit. Returns the same value as operator []''. The difference is when the index of the element to be accessed exceeds the array size limit. On `at (),` the program will throw an `out_of_range` error when running. On the operator []``, this causes an undefined behavior.
  • front() - Accesses the first position element.
  • back() - Accesses the element in the last position.
  • begin() - An iterator for the start of the sequence.
  • end() - An iterator for the element after the end of the sequence.
  • size() - Get the number of elements.
  • max_size() - Get the maximum number of elements the array can hold. Returns the same value as size ().
  • empty() - Checks whether the array is empty or not. If array size is 0, returns true. Otherwise, returns the value false.
  • swap() - Swaps all elements in an array with all elements in another array that have the same data type and size.
  • fill() - Fills all the elements in the array with a specific value.

std::vector

Do you still remember [Dynamic Array] (https://github.com/AlproITS/StrukturData/wiki/Modul-0-(Dynamic-Array))? std::vector is a type of data structure that represents an array with the ability to change size (capacity) dynamically according to the amount of data entered. Operations on std::vector:

  • operator[] - Accesses the element at a specific position.
  • at() - Accesses element in specific position while checking vector size limit. Returns the same value as the operator []. The difference is when the index of the element to be accessed exceeds the vector size limit. On at (), the program will throw an out_of_range error when running. On the operator[], this causes an undefined behavior.
  • front() - Accesses the first position element.
  • back() - Accesses the element in the last position.
  • begin() - An iterator for the start of the sequence.
  • end() - An iterator for the element after the end of the sequence.
  • size() - Get the number of elements.
  • max_size() - Get the maximum number of elements the vector can hold.
  • resize() - Resizes a vector to a specified number of elements. resize () can also be accompanied by specifying a specific value, but it will not change an element that has a previous value (as opposed to assign ()).
  • empty() - Checks whether the vector is empty or not. If the vector size is 0, returns true. Otherwise, returns the value false.
  • assign() - Set the vector size to be a certain number of elements with a certain value on all elements.
  • push_back() - Adds the element at the last position.
  • pop_back() - Deletes the last element.
  • insert() - Inserts a value at a specific position (or range).
  • erase() - Deletes the value at a specific position (or range).
  • clear() - Removes all elements, so the vector size is 0.
  • swap() - Swaps all elements in a vector with all elements in another vector that have the same data type (size can be different).
  • sort(first, last) - Sorts the elements in the array by descending in the range (first, last).
  • lower_bound(first, last, val) - Returns an iterator that points to the smallest element not less than $ val $ in the range (first, last). If it doesn't exist, it returns the last iterator.
  • upper_bound(first, last, val) - Returns an iterator that points to the smallest element that is more than $ val $ in the range (first, last). If it doesn't exist, it returns the last iterator.

Navigasi

Home

Modul 0

Modul 1

Modul 2

Modul 3

  • Self-Balancing Binary Search Tree IND | ENG
  • AVL Tree IND | ENG

Modul 4

Modul 5

Clone this wiki locally