STL

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/46

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

47 Terms

1
New cards

what does the STL do?

separates Algorithms from Data to allow for Code Reuse

2
New cards

what are the 3 categories within the STL?

algorithms, iterators, containers

3
New cards

what are containers? types of containers?

  • containers are data structures that hold objects/data of arbitrary type

  • types

    • sequence containers

    • associative containers (ordered and unordered)

    • container adapters

4
New cards

what are iterators? types?

  • iterators are objects of arbitrary type that can step through elements of containers

  • types

    • input

    • output

    • forward

    • bidirectional

    • random access

5
New cards

what are algorithms? types?

  • algorithms are common operations used to manipulate elements in data structure

  • types

    • mutating

    • non-mutating

    • sorting

    • numeric

    • removing

6
New cards

sequence containers

  • implement data structures that can be accessed sequentially

  • data arranged in a linear manner

  • element order is independent from its value

  • implemented using arrays or linked lists

  • ex: vector, deque, list, array, forward_list

7
New cards

associative containers

  • ordered

    • data arranged to facilitate fast associative operations

    • keys ordered by operator< to support efficient operations on elements

    • implemented using (balanced) binary search trees

    • ex: set, multiset, map, multimap

  • unordered

    • unsorted collections

    • position of elements is irrelevant

    • implemented using hash tables

    • ex: unordered_set, unordered_multiset, unordered_map, unordered_multimap

8
New cards

container adapters

  • are NOT containers

  • provide different ways to access sequential and associative containers

  • ex: stack, queue, priority_queue

9
New cards

details of array?

  • a sequence container

  • Static contiguous array (class template)

  • array based

10
New cards

details of vector?

  • a sequence container

  • Dynamic contiguous array (class template)

  • array based

11
New cards

details of deque?

  • a sequence container

  • Double-ended queue (class template)

  • array based

12
New cards

details of forward_list?

  • a sequence container

  • Singly-linked list (class template)

  • doubly-linked list based

13
New cards

details of list?

  • a sequence container

  • Doubly-linked list (class template)

  • doubly-linked list based

14
New cards

std::array

  • encapsulates FIXED-SIZE arrays

  • supports const and mutable random access iterators

  • amortized constant time insert/erase at its end

  • linear time insert/erase in its middle

  • contiguous storage for all types including bool

15
New cards

std::vector

  • implemented as a dynamically allocated array (can grow and shrink)

  • provides pre/re allocation, indexed storage, push_back, pop_back

  • supports const and mutable random access iterators

  • when capacity is reached, a new array is made with double the capacity and the data is copied into it

  • common operations: push_back, emplace_back, at, size, clear

  • advantages

    • contiguous memory = fast insert/remove from end

    • insert/remove more expensive if at beginning or middle

16
New cards

std::deque

  • double-ended queue

  • efficient O(1) insertion and removal at both ends via push_front() and pop_front()

  • supports const and mutable random access iterators

  • linear time insert/erase in its middle

17
New cards

std::forward_list

  • has constant time insertion and deletion at any point in the sequence

  • does not support random access iterator

  • includes member functions for splicing

  • implemented as a singly-linked list

18
New cards

std::list

  • has constant time insertion and deletion at any point in the sequence

  • does not support random access iterator

  • supports const and mutable bidirectional iterators instead

  • includes member functions for splicing, sorting, and merging

  • implemented as a doubly-linked list

19
New cards

what is a set? std::set

  • associative container

  • an ordered collection containing no duplicate elements

  • supports testing for membership, adding elements, and removing elements

  • set operations: union, intersection, difference, subset

  • value of elements cannot be modified

20
New cards

what is a map?

  • associative container

  • a set of ordered pairs containing a key and value (like python dictionary)

  • keys map to values

  • different keys can be mapped to the same value

21
New cards

ordered associative containers (2 qualities)

  • values are “associated” with a key

  • elements are automatically sorted

    • default is <

    • users can define own ordering

    • implemented using a binary search tree

    • no push_back or push_front

    • O(log(n)) searching most important feature

22
New cards

properties of std::set and std::multiset

  • associative containers

  • fast search: O(log(n))

  • value of elements cannot be modified

23
New cards

what can associative containers std::multiset and std::multimap do?

support multiple equivalent (non-unique) keys

24
New cards

what is std::pair and what does it do

  • STL helper class

  • basis for associative containers

  • can store heterogeneous pairs of data together

  • binds first element (key) to second element (associated value)

25
New cards

what do associative containers std::map and std::multimap have in common?

don’t allow for duplicate KEYS

26
New cards

how do unordered containers store info (because they’re not stored in order)

  • a hash function takes inputs and regurgitates it as an address in memory to place info

  • if you don’t have a good hash function —> collision: when two or more items are assigned the same location in memory

27
New cards

properties of unordered containers

  • fastest search/insert at any place: O(1)

  • unordered set/multiset: element value cannot be changed

  • unordered map/multimap: key cannot be changed

28
New cards

properties of std::unordered_set

  • (unordered associative container)

  • amortized constant time element lookup

  • hash determines order of key

  • includes const forward iterator

  • elements are unique

  • implemented as a hash table

29
New cards

properties of std::unordered_multiset

unordered set that allows duplicate elements

30
New cards

properties of std::unordered_map

  • amortized constant time lookup of type T based on type Key

  • implemented as a hash table; hash determines order of Key

  • more efficient than map for element access, less for range iteration

  • includes at least forward iterator

  • keys are unique

31
New cards

properties of unordered_multimap

cannot use [] operator to access bc it can contain duplicate keys

32
New cards

how do stacks work?

  • Last In First Out

    • can only access top of the stack

  • elements are pushed/popped from the container

    • push adds an item to the top

    • pop removes an item from the top

  • may be represented using a STL container adaptor or vector/deque directly

33
New cards

how do queues work?

  • First In First Out

  • elements are inserted at the back and deleted from the front

  • may be represented using the STL container adapter, a deque, or a linked list

34
New cards

what are STL iterators?

  • an implementation of the iterator pattern

    • provides access to elements of an aggregate object sequentially

    • does not expose underlying representation of the object

    • encapsulates internal structure of iteration

  • most obvious form is a pointer (iterate through array elems via ++), but not all iterators have similar funcitonality as that of pointers

  • used to iterate over a range of objects

  • the interface between containers and algorithms

  • central to generic programming

  • algorithms take iterators as arguments

  • containers provide access to elements using iterators

35
New cards

5 categories of iterators

weakest

  • input and output

  • forward

  • bidirectional

  • random access

strongest

36
New cards

input and output iterators

weakest and can only be used in single pass algorithms

input: no element accessed more than once, readonly

output: used for assigning elements, write only

37
New cards

forward iterators

are both input and output iterators. can only move in forward direction and only one step at a time. read and write with revisiting capabilities.

38
New cards

bidirectional iterators

are forward iterators, but can move in both directions

39
New cards

random access iterators

are bidirectional iterators. most powerful and not limited to move sequentially (most pointer-like behavior). can randomly access any element in the container by index

40
New cards

native types

(pointers)

can be used as iterators

41
New cards

iterator benefits

  • convenience

  • reusability

  • dynamic processing of container

42
New cards

generic algorithms

  • operate on iterators rather than containers

  • containers declare iterator and const_iterators traits

  • containers declare factory methods for their iterator types

  • templates provide compile-time type safety

43
New cards

categories of generic algorithms

  • non-mutating: do not change the data elements

  • mutating: can change the data elements

  • sorting and search: sort or search and act on ranges of elements

  • numeric: mutating algorithm which produce numeric results

  • specific algorithms that accept a predicate condition

44
New cards

benefits of STL generic algorithms

  • decoupled from containers they operate on

  • parameterized by iterators

  • all containers with same iterator can use same algorithms

  • software effort drastically reduced

  • just a few versions of the search routine must be implemented

45
New cards

STL function objects, aka functors

  • function objects are objects that can be called like functions

  • they overload the function call operator ()()

  • they work where functions are expected

  • public access must be granted to the overloaded () operator, which must be declared and defined

  • they hold state and customize behavior

46
New cards

lambda expressions

  • compiler provided functor without the boiler plate syntax

  • inline anonymous unnamed function

  • simple throwaway function without the intention of reuse

  • easy to read, clean

47
New cards

format of lambda expression

[ captures ] ( params ) specs requires (optional) { body }
  • [ ] capture clause

  • ( ) input parameters w/ datatypes

  • { } function body