Containers

Lambda Examples

Lambda example

#include <iostream>

template<typename F>
int foo(int x, int y, F lambda) {
    return lambda(x,y);
}

struct Multiply{
    double operator()( const double v1, const double v2 ) const{
        return v1 * v2;
    }
};

int main(){
    std::cout << foo(2,3, [](int x, int y){return x*y;}) << std::endl;
    Multiply mul;
    std::cout << foo(3,4, mul) << std::endl;
    return 0;
}

Really looks like this

Example 2

Really looks like this:

for_each

I usually will use a range-based for (for(auto& n : nums)) instead of for_each

Containers

Sequence containers

Sequence containers implement data structures which can be accessed sequentially.

name

description

array

static contiguous array

vector

dynamic contiguous array

deque

double-ended queue

forward_list

singly-linked list

list

doubly-linked list

Associative containers

Associative containers implement sorted data structures that can be quickly searched (O(log n) complexity).

name

description

set

collection of unique keys, sorted by keys

map

collection of key-value pairs, sorted by keys, keys are unique

multiset

collection of keys, sorted by keys

multimap

collection of key-value pairs, sorted by keys

Unordered associative containers

Unordered associative containers implement unsorted (hashed) data structures that can be quickly searched (O(1) amortized, O(n) worst-case complexity).

name

description

unordered_set

collection of unique keys, hashed by keys

unordered_map

collection of key-value pairs, hashed by keys, keys are unique

unordered_multiset

collection of keys, hashed by keys

unordered_multimap

collection of key-value pairs, hashed by keys

Container adaptors

Container adaptors provide a different interface for sequential containers.

name

description

stack

adapts a container to provide stack (LIFO data structure)

queue

adapts a container to provide queue (FIFO data structure)

priority_queue

adapts a container to provide priority queue

Span (C++20)

name

description

span

a non-owning view over a contiguous sequence of objects

More infoarrow-up-right

Sequence containers

array

Creating array

Element access & Capacity

container operations are supported

ranged for loop is supported

Most modifiers are not supported such as push_back

vector

  • The storage of the vector is handled automatically, being expanded and contracted as needed.

  • The elements are stored contiguously.

  • Element access, capacity, container operations and ranged-loops are supported

Creation

Modifiers

deque

  • deque (double-ended queue) is an indexed sequence container that allows fast insertion and deletion at both its beginning and its end O(1)

  • not stored contiguously

  • Expansion of a deque is cheaper than the expansion of a std::vector

    • cons: large minimal memory cost

  • Random access - O(1)

  • Element access, capacity, container operations and ranged-loops are supported

A deque is somewhat recursively defined: Internally it maintains a double-ended queue of chunks of fixed size. Each chunk is a vector, and the queue (“map” in the graphic below) of chunks itself is also a vector.

Sourcearrow-up-right

list

  • constant time insertion and removal of elements from anywhere in the container.

  • Fast random access is not supported

  • Usually a doubley-linked list

Element access

  • supports mostly everthing in vector except for [], at

doubly_linked_list.jpg

Examples

More examples: std::reduce,std::transform_reduce

Filling in numbers

std::iota-fill container with range

Conditional

all_of(), none_of(),count_if,find_if

Associative Containers

set

Sorted data structures that can be quickly searched (O(log n) complexity).No duplicates

Important! as with any sorted container we need to give the set a Compare class such as less<int> or greater<int> by default Compare = std::less<T>

multiset

Similar to set, with an exception that multiple elements can have same values.

map

  • Each element has a key value and a mapped value.

  • No two mapped values can have same key values.

  • keys are sorted

Example 2

This also works

multimap

Multimap is similar to map with an addition that multiple elements can have same keys.

Unordered associative containers

unordered_set

unordered_map

Span (C++20)

The following code helps us with one-off errors.

Besides of ranged-based for loops, we also have find_if etc but without container overhead. Pretty cool 😎

Valarray

Difference between std::pair and std::tuple with only two members?

  • std::pair can only have two values - not zero, one, three or more

  • It's a bit easier to get the contents of a pair than a tuple. You have to use a function call in the tuple case: get<0>(t), while the pair case is just a member field (p.first,p.second)

Unpacking

auto [x,y] = f_pair() Creates this implicitly

Unpacking Structed Bindings

Allows unpacking any data structure whose size is known during compile time

Creditsarrow-up-right

basic_string and string

A std::string is an instantiation of the std::basic_string template with a type of char

without overloading the global operator<< we would have been able to print ints

Last updated