Chapter 15 - Worked Exercises 2
Exercise 1 - Vectors
Part 1
Internally, how are the elements of a vector stored or represented? Are they contiguously in memory?
What operations are quick for vectors?
What operations are relatively slow for vectors?
Part 2
Given the following vector declaration:
std::vector<int> numbers = {1, 2, 3, 5, 7, 11, 13};Enumerate through the vector, and print each element:
using a for-loop with indexing
using a range-based loop
using iterators
Part 3
Accessors
Do vectors support random access (indexing)?
How can you access the value of a vector at index
3?
Capacity
How do you check if a vector is empty?
How do you get the capacity and size of a vector?
What is the difference between the capacity of a vector and its size?
Can the capacity of a vector ever be smaller than its size?
Modifiers
Show an example of inserting the value
100at index3of a vector.Show an example of inserting multiple elements from one vector into another.
Show an example of removing the value at index
5of a vector.Show an example of removing the values from index
2to index4(non inclusive of4) from a vector.Show an example of removing all elements from a vector.
Last updated
Was this helpful?