Part 6 - Templates, Containers, Vectors, Iterators, STL Algorithms Flashcards
What does
template
above a method indicate?
T is a type parameter that can be instantiated with any type
So at compile time a version of the method will be generated for each type that uses it
Write a template version of the swap function
template
void swap(T& a, T& b) {
T tmp = a;
a = b;
b = tmp;
}
Write a method that prints a pair where each member of the pair can be of a different type
template
void printPair(const S& s, const T& t) {
cout << s << “, “ << t << endl;
}
What is a template class?
A class where one of the members can be of any type
Write the header for a class Pair that has two members of any type, a constructor and two methods getFirst and getSecond
template
class Pair {
private:
S first;
T second;
public:
Pair(S s, T t) : first(s), second(t){};
S getFirst() const { return first; }
T getSecond() const { return second; }
};
Why must a template class be written as one file?
Writing a separate file wont work because the type names S or T or whatever wont be in scope in the .cpp file. So has to be written in the header.
what is a container in STL?
A template class in the STL that contains a collection of objects and algorithms
What are the 3 sequence containers (that store sequential data) in the STL?
vector
deque
list
What are container adapters?
Containers that do not provide full functionality because their design means some operations may not apply
What are the 3 container adapters?
stack
queue
priority_queue
What are the 4 associative containers?
set
multiset
map
multimap
What does the no-arg constructor do for all STL containers?
copy constructor?
initialise the collection to be empty
initialise a collection to contain copies of the objects in the existing collection
What 4 functions do ALL classes in STL have? What do they do?
size(), empty(), max_size() and swap()
size() returns number of items in collection
empty() returns true if 0 items else false
max_size() returns max possible size for container
swap() takes reference to another container of same type and swaps the contents of the two containers
What does the clear() function do with containers?
Sets the collection to be empty
What does the vector class do? What header is needed?
Parametrised Array with range checking
what will vector v3(10, ‘x’); do?
Create a vector v3 that contains 10 x’s
Why use .at over [] for vectors?
.at has range checking
what does vector.reserve(100) do?
Increases the capacity of vector to 100
If a vector is resize() ed to a smaller size than its current size what happens?
Elements after the resized size get deleted
What are the 3 types of iterator?
unidirectional (++), bidirectional (++ and –) and random access (full poitner arithmetic)
Write a function to iterate over a vector using a const iterator
void testFunc(const vector& v) {
vector::const_iterator it;
for (it = v.begin(); it != v.end(); it++) {
cout << *it << endl;
}
}
Write a function to iterate over a vector backwards using a const iterator
void printBackwards(const vector& v) {
vector::const_reverse_iterator it;
for (it = v.rbegin(); it != v.rend(); it++) {
cout << *it << endl;
}
}
How does the erase function work with STL containers?
takes a non const iterator and removes the item it is pointing to
Write a function to remove the first instance of 0 in a vector
void removeFirst0(vector& v) {
vector::iterator it;
for (it = v.begin(); it != v.end(); it++) {
if (*it == 0) {v.erase(it); return;}
}
}
Why can one iterator not be used in conjunction with erase() to remove more than one instance of a given thing?
The iterator value after erase() is called is undefined
What does the two arg version of erase() do?
Removes all items in a range
e. g.
v. erase(v.begin(), v.begin+3); would remove the first 3 elements
How do typedefs work?
typedef theactualtype alias;
e.g.
typedef vector::const_reverse_iterator CRI;
then you could use
CRI it = v.begin();