Day 23 Recap Questions
- What is a template in C++?
- What is the standard template library (STL)?
- How do you iterate over a
std::vector
and print out its elements? - What is an iterator in C++?
- How do you add an element to an existing vector.
- What’s the difference between an
iterator
and aconst_iterator
? - (Bonus) What is the output of the program below?
#include <iostream>
#include <vector>
using std::cin; using std::cout; using std::endl;
using std::vector;
int main() {
vector<double> numbers;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 1)
numbers.insert(numbers.begin(), i / 2.0);
else
numbers.push_back(i * 2.0);
}
vector<double>::iterator it = numbers.begin();
cout << "first == " << *it << endl;
cout << "middle1 == " << *(it + 4) << endl;
cout << "middle2 == " << *(it + 5) << endl;
cout << "last == " << *(it + 9) << endl;
}