Effective STL Flashcards
What are the STL sequence containers?
vector, string, deque, list.
What are the STL associative containers?
map, set, multimap, multiset.
What are the contiguous-memory containers?
Containers that store their elements in one or more chunks of memory, each chunk holding more than one container element.
What are the node-based containers?
Containers that store only a single element per chunk of memory.
What are some of the contiguous-memory containers?
vector, string, deque.
What are some of the node-based containers?
list and all associative containers.
What type of iterators do contiguous-memory containers provide?
Random access iterators.
What type of iterators do node-based containers provide?
Bidirectional iterators.
What type of iterators does std::sort require?
Random access iterators.
Can std::map be used with std::sort algorithm?
No
Why can’t std::list be used with std::sort algorithm?
Because list uses bidirectional iterators and sort requires random access iterators.
Why is empty() prefered over size() == 0 check for container sizes?
Because empty is a constant-time evalutation and size can be linear operation and thus more innefficient.
What is a range member function?
Function taking two iterator parameters to speficy range of elements over which something should be done.
What are the costs of invoking single-element container functions?
Unecessary function calls (in a loop), memory allocation, innefficieny of moving elements in the container while inserting new elements.
What are the advantages of using range member functions?
Easier to write, more clear intent, higher performance
What does following statement declare: int f(double d);
Function taking double and returning an int.
What does following statement declare: int f(double (d));
Function taking double and returning an int, () are ignored.
What does following statement declare: int f(double);
Function taking double and returning an int, param name is omittted.
What does following statement declare: int g(double ());
Function taking a pointer to a function returning double with 0 args.
What does following statement declare: int g(double pf());
Function taking a pointer to a function returning double with 0 args, pf is implicitly a pointer.
What does following statement declare: int g(double (*pf)());
Function taking a pointer to a function returning double with 0 args, pf is explicitly marked as pointer.
What are two ways for making sure dynamically allocated objects in the container are freed?
Smart pointers or explicit delete invokation on each container element.
What is the time complexity of invoking erase for map and set?
Logartihmic
What is the time complexity of invoking erase for multimap and multiset?
Linear for a number of elements contained.
What is the recommended approach for removing all objects in a list that have particular value?
Use list’s remove member function because it is more efficient than std::remove.
What is the recommended approach for removing all objects in a contigous-memory container that have particular value?
Use erase-remove idiom -> Erase member function with std::remove for range.
What is the recommended approach for removing all objects in a associative container that have particular value?
Use erase member function of the container.