Effective STL Flashcards

1
Q

What are the STL sequence containers?

A

vector, string, deque, list.

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

What are the STL associative containers?

A

map, set, multimap, multiset.

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

What are the contiguous-memory containers?

A

Containers that store their elements in one or more chunks of memory, each chunk holding more than one container element.

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

What are the node-based containers?

A

Containers that store only a single element per chunk of memory.

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

What are some of the contiguous-memory containers?

A

vector, string, deque.

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

What are some of the node-based containers?

A

list and all associative containers.

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

What type of iterators do contiguous-memory containers provide?

A

Random access iterators.

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

What type of iterators do node-based containers provide?

A

Bidirectional iterators.

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

What type of iterators does std::sort require?

A

Random access iterators.

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

Can std::map be used with std::sort algorithm?

A

No

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

Why can’t std::list be used with std::sort algorithm?

A

Because list uses bidirectional iterators and sort requires random access iterators.

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

Why is empty() prefered over size() == 0 check for container sizes?

A

Because empty is a constant-time evalutation and size can be linear operation and thus more innefficient.

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

What is a range member function?

A

Function taking two iterator parameters to speficy range of elements over which something should be done.

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

What are the costs of invoking single-element container functions?

A

Unecessary function calls (in a loop), memory allocation, innefficieny of moving elements in the container while inserting new elements.

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

What are the advantages of using range member functions?

A

Easier to write, more clear intent, higher performance

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

What does following statement declare: int f(double d);

A

Function taking double and returning an int.

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

What does following statement declare: int f(double (d));

A

Function taking double and returning an int, () are ignored.

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

What does following statement declare: int f(double);

A

Function taking double and returning an int, param name is omittted.

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

What does following statement declare: int g(double ());

A

Function taking a pointer to a function returning double with 0 args.

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

What does following statement declare: int g(double pf());

A

Function taking a pointer to a function returning double with 0 args, pf is implicitly a pointer.

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

What does following statement declare: int g(double (*pf)());

A

Function taking a pointer to a function returning double with 0 args, pf is explicitly marked as pointer.

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

What are two ways for making sure dynamically allocated objects in the container are freed?

A

Smart pointers or explicit delete invokation on each container element.

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

What is the time complexity of invoking erase for map and set?

A

Logartihmic

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

What is the time complexity of invoking erase for multimap and multiset?

A

Linear for a number of elements contained.

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

What is the recommended approach for removing all objects in a list that have particular value?

A

Use list’s remove member function because it is more efficient than std::remove.

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

What is the recommended approach for removing all objects in a contigous-memory container that have particular value?

A

Use erase-remove idiom -> Erase member function with std::remove for range.

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

What is the recommended approach for removing all objects in a associative container that have particular value?

A

Use erase member function of the container.

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

What is the recommended approach for removing all objects in a list that satisfy a particular predicate?

A

Use list’s remove_if member function.

29
Q

What is the recommended approach for removing all objects in a contigous-memory container that satisfy a particular predicate?

A

Use erase-remove_if idiom.

30
Q

What is the recommended approach for removing all objects in a associative container that satisfy a particular predicate?

A

Use remove_copy_if with swap or write a loop to walk the container elements, being sure to postincrement your iterator when you pass it to erase.

31
Q

What “problems” does vector growth bring?

A

It invalidates all pointers and references to its internals because the content is relocated.

32
Q

Why is vector bool not considered an STL container?

A

It does not fully conform to C++ standard rules for containers. operator[] does not return a reference to bool object.

33
Q

How are bools stored internally in vector?

A

Stored like bitfields.

34
Q

What does vector bool operator[] return?

A

Reference to a proxy object that wraps the bitfield representation of bool.

35
Q

What does equality means in terms of associative containers?

A

Equality is based on operator==.

36
Q

What is equivalence in terms of associative containers?

A

It is based on relative ordering of objects values in a sorted range. Two vales are equivalent if neither precedes the other on, according to some ordering criterion.

37
Q

What is the default comparison for associative containers?

A

std::less

38
Q

How are associative containers usually implemented?

A

As balanced binary search trees.

39
Q

What is a drawback for using sorted vector instead of an associative container?

A

Keeping it sorted on lots of insertions and erasures.

40
Q

What is the potential benefit of using a sorted vector instead of an associative container?

A

Less memory consumption and faster access (binary search).

41
Q

What does std::partial_sort do?

A

Sorts n number of elements per specified criteria. Remaining elements (n+1, n+2) won’t be sorted.

42
Q

What does std::nth_element do?

A

Finds an nth element and puts it to its nth position. All the elements that would follow up to that element are put before it.

43
Q

What does std::partition do?

A

Finds all the elements that match the specified criteria and moves them to the beginning of the vector.

44
Q

What is meant by stable sort?

A

Equivalent elements will be unchanged (their position won’t be changed) when sorted with stable sort.

45
Q

What does std::remove do?

A

It moves the “to be removed” elements to the end of the range and returns an iterator pointing to the first element to be removed.

46
Q

What does std::accumulate predicate must ensure?

A

It must not have side effects.

47
Q

Function objects are modeled on what?

A

Function pointers.

48
Q

How should function objects be passed?

A

By value.

49
Q

What are the two things that should be taken into account when defining function objects?

A

They should be small (cheap to copy) and monophormic (not use virtual functions).

50
Q

What is an advantage of function objects over function pointers?

A

They can hold more state (via members).

51
Q

What is a predicate?

A

A function that returns a bool (or something implicitly convertable to bool).

52
Q

What is a pure function?

A

Function whose return value depends only on its input parameters.

53
Q

What is a predicate class?

A

Class whose operator() returns bool or something implicitly convertable to bool.

54
Q

Why should operator() be marked as const?

A

To enforce imutability.

55
Q

Why should std components never be modified or specialized (templates) ?

A

Because doing so can lead to UB.

56
Q

What are the three reason describing why should algorithms be prefered over hand-written loops?

A

Efficiency, maintainability, correctness.

57
Q

Why should member functions be prefered over std::algorithms with same name?

A

They are often faster and integrate better with the containers.

58
Q

What is the time complexity of std::set find?

A

Logarithmic.

59
Q

What is the time complexity of std::find ?

A

Linear.

60
Q

What are the two things that one should consider when choosing between std algorithms and member functions?

A

Efficiency and correctness.

61
Q

What is the time complexity of count and find?

A

Linear.

62
Q

On what type of ranges should count and find be used for lookup?

A

On unsorted ranges.

63
Q

On what type of testing are count and find based on?

A

Equality testing.

64
Q

On what type of ranges should binary_search, lower_bound, upper_bound, equal_range be used?

A

On sorted ranges.

65
Q

On what type of testing are binary_search and other similar algorithms based on?

A

Equivalence testing.

66
Q

When should std::count be used?

A

When we want to see how many copies of certain value are there in the range.

67
Q

When should std::find be used?

A

When we want to see if certain value exists and where it is.

68
Q

What algorithm is more efficient, count or find?

A

Find because it returns when it finds the element.