Containers
The Standard Library containers allow you to group together zero or more items of the same type and access them serially through iterators. Every such object has a begin
method that returns an iterator object to the first item and an end
function that returns an iterator object for the item after the last item in the container. The iterator objects support pointer-like arithmetic, so that end() - begin()
will give the number of items in the container. All container types will implement the empty
method to indicate if there are no items in the container, and (except for forward_list
) the size
method is the number of items in the container. You are tempted to iterate through a container as if it is an array:
vector<int> primes{1, 3, 5, 7, 11, 13}; for (size_t idx = 0; idx < primes.size(); ++idx) { cout << primes[idx] << " "; } cout << endl;
The problem is that not all containers allow random access, and if you decide it...