C++ Standard Library Containers Flashcards

1
Q

Why do we want containers?

A

To avoid consuming time and effort when writing memory efficient and performant data structures.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is OOP about?

A

Producing reusable code, that encapsulates data implementation and abstraction of inner workings ( as well as inheritance and polymorphism)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are built-in containers an example of?

A

Template metaprogramming

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What do containers provide?

A

Well-tested code with guaranteed time and memory complexities.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are some advantages of using containers?

A
  • Provides a common and consistent interface for accessing data in different container types
  • Allows for easy swapping between containers without having to change too much code
    -Makes code more readable
  • Generalises passing data to standard algorithms.
  • Once behaviour is encoded once it can be easily reused.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are derived containers?

A

Containers that are built upon existing container classes, inheriting their functionality and extending it to provide additional features of customization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is template metaprogramming?

A

A technique that leverages compile-time computation using templates to perform complex tasks and computations, allowing for efficient and flexible code generation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the advantages of derived containers?

A
  • Code reusability
  • Customisation
  • Abstraction
  • Efficiency
  • Consistency
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What order do associative containers store items in?

A

Order or unordered variants.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do we order containers for our own types/classes?

A

Using operator overload.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When should we use the std::array container?

A

Fixed size array small enough to go on the stack.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When should we use std::vector container?

A
  • When dealing with small data elements with mostly push/pop_back modification
  • Large elements where the number of elements is constant.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When should we use std::deque?

A

Front and back modifications are needed. Less memory efficient but fast front modifications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When should we use std::list or std::foward_list?

A

When adding, removing or randomly inserting elements. (Forward list if only integrating forward). But not specific search for one element, as list works as a linked list.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When should we use std::set?

A

For ordering unique elements and fast look/insertion.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

When should we use std::map?

A
  • Mapping non-sequential integers or hashable type to some other value.
17
Q

When should we use std::onordered_set and std::onordered_map?

A

When fast access/insertion is required and order of traversal is not important

18
Q

What does std::set do?

A

Only allows unique elements. It internally orders the elements so they are iterated in a sorted order.

19
Q

What are std::lists?

A

Double-linked list, each elements points to the previous and next item in the list. Meaning we don’t have to store things contiguously in memory. Can be iterated backwards of forwards

20
Q

What is an example of a derived container?

A

Stack
std::stack<T, Container = std::Dequeue<T>></T>

21
Q

What are the time complexities of derived containers?

A

The time complexity of the underlying structure.

22
Q

What are associate containers?

A

Containers that map keys of “some” type to another type. Examples are sets and maps.

23
Q

What must associate keys be?

A

Hashable

24
Q

What is the time complexity of accessing a set?

A

O(log n)

25
Q

How do we find elements in a set?

A

Using the find operator
auto it = set.find(4)
If it isn’t null value or != set.end has been found

26
Q

What are maps?

A

Maps store the value along with a key to uniquely identify the value.