STLs

Revision en1, by salama305, 2024-11-03 15:15:04

STL (Standard Template Library):

** Vector **
- Definition: A dynamic array that can change in size.
- Syntax: `vector <data_type> name(size);`
- By default, all elements are initialized to zero if no value is specified.
- Available functions:
    - Add an element to the end: `name.push_back(value);`
    - Copy elements from another vector: `vector<data_type> v2(v);`
    - Swap elements between two vectors: `v1.swap(v2);`

** Deque (Double Ended Queue) **
- Definition: Allows insertion and deletion of elements from both ends.
- Syntax: `deque<data_type> name;`
- Available functions:
    - Add an element to the back: `push_back();`
    - Add an element to the front: `push_front();`
    - Remove an element from the back: `pop_back();`
    - Remove an element from the front: `pop_front();`
    - Remove an element at a specific INDXition: `erase(name.begin() + n);`

** List **
- Definition: A linked list that stores elements with access from the front and back.
- Syntax: `list<data_type> name(size);`
- Available functions:
    - Access the first element: `front();`
    - Access the last element: `back();`
    - Remove all elements with a specific value: `name.remove(value);`
    - Traverse through elements: `auto it = name.begin(); it++;`
    - Remove an element using an iterator: `name.erase(it);`
    - Insert an element at a specific position: `name.insert(pos, value);`
    - Remove elements that meet a specific condition: `name.remove_if(condition);`

** Set **
- Definition: A collection of unique, sorted elements.
- Characteristics:
    - Does not support direct element insertion methods (like `push_back` or `push_front`).
    - Does not support random access with indices (`[]`).
    - Insert an element: `st.insert(value);`
- Additional information:
    - Access the first element (smallest value) using: `set.begin()`
    - Access the last element (largest value) using: `--set.end()`

** Map **
- Definition: Stores pairs of keys and values, where each key is unique and maps to a specific value.
- Syntax: `map<key_type, value_type> nameMap;`
- Available functions:
    - Remove an element using an iterator: `erase(iterator);`
    - Remove a range of elements: `erase(from, to);`
    - Access or insert a value by key: `nameMap[key]`

** Multiset **
- Definition: Similar to a set, but allows storing duplicate values.

** Additional Information about STL Containers **
- Associative containers like set and map use binary tree structures internally.
- These containers provide fast access to elements.
- Elements in a set are ordered, and duplicate values are not allowed.
- Random access like arrays (e.g., `[]`) is not supported in these containers.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English salama305 2024-11-03 15:15:04 2964 Initial revision (published)