Refresher on Functions and Classes 1 Flashcards
What is an Iterator in C++?
An object that can traverse a container class without the user knowing how the container is implemented.
Examples include std::map and std::vector.
What types of iterators do containers provide?
- iterator: read/write iterator
- const_iterator: read-only iterator
What does the begin() function return?
An iterator representing the beginning of the elements in the container.
What does the end() function return?
An iterator representing the element just past the end of the elements.
What is the purpose of the dereference operator (*) for iterators?
To return the element that the iterator is pointing at.
What does the operator++ do for iterators?
Moves the iterator to the next element in the container.
What is a set in C++?
A collection of unique values containing no duplicates, stored in sorted order.
What happens when you try to insert a duplicate value into a set?
The duplicate value does not get added.
What does the find() method do in a set?
Returns an iterator pointing to the element if it exists or end() if it does not.
What is a map in C++?
An associated container that has a key/value mapping with unique keys.
What is the difference between a set and a map?
A map has a value associated with each unique key, while a set only contains unique values.
What does the insert() method expect when adding to a map?
An std::pair representing the key and value.
What is a function template in C++?
A template that allows the creation of functions that can operate on different data types.
What is the syntax to declare a template for a single type?
template <typename></typename>
What is the purpose of the Rule of 3 in C++?
It states that if a class manages resources, it should define a destructor, copy constructor, and copy assignment operator.
What does the term ‘deep copy’ mean?
Creating a new copy of an object along with all objects it references.
What is meant by ‘shallow copy’?
Copying an object while sharing references to the same resources it points to.
What is the purpose of a constructor in a C++ class?
To initialize an object when it is created.
What does the term ‘accessor’ refer to in C++ classes?
A member function that retrieves the value of a private member variable.
Fill in the blank: A _______ is an object that can traverse a container class.
Iterator
True or False: A set can contain duplicate values.
False
What is the output of the following piece of code? std::vector<int> nums = {10, 20, 30, 40, 50}; std::vector<int>::iterator it = nums.begin(); while (it != nums.end()) { if (*it % 20 == 0) { it = nums.erase(it); } else { ++it; } } for (it = nums.begin(); it != nums.end(); ++it) { std::cout << *it << ' '; }</int></int>
10 30 50
What does the erase() function do in a vector?
Removes an element from the vector and returns an iterator to the next element.
What data structure is assumed to be used in the set class?
Binary Search Tree
This could have been any data structure.