Some Additional Things
- clear will remove all items from the vector.
- erase will take one or two iterators
- If just one iterator, it will remove the item in the vector pointed at by that iterator.
- It will return an iterator pointing to the item that followed the item erased.
- If two iterators are passed in, all values between these iterators will be erased. This includes the first but not the last.
- Any iterators pointing to items after the deleted item are invalid
- But they still possibly point to something.
- This is an O(n) operation, so swap and pop_back are much cheaper.
- insert has several forms.
- Given an iterator and a value
- Insert the value before the position.
- Given an iterator, a value and a count
- Insert count copies of value before the iterator.
- Given an iterator, and two iterators into another vector
- Insert all elements between the two iterators into the vector.
- Return an iterator to the first inserted value.
- All iterators may be invalidated
- O(n).
- There are some really useful functions in the standard algorithm library.
- you need to #include <algorithm>
- find
- Assumes the items in the container support ==
- This is the standard find function.
- It takes an iterator to the beginning of a range.
- And an iterator to one past the end of a range.
- And an item.
- It will return an iterator to the item, or an iterator pointing to the end of the range.
- O(n)
- sort
- assumes the items in the container support <
- Takes two iterators, or two iterators and a compare function.
- Sorts the container from the first to one less than the last.
- Performance O(n lg(n))
- binary_search
- Uses the < operator.
- Note: a == b if (!(a<b) && !(b<a)) or if (!comp(a,b) && !comp(b,a))
- Takes two iterators and a value.
- Or two iterators, a value and a comparison operator
- Returns true if an item is found in the container.
- THE CONTAINER MUST BE SORTED
- Performance O(lg(n))
- There are a few that are really useful
- max and min take two items with the < operator and return the max or min.
- clamp takes a value, a low and a high and
- Returns the value if it is between low and high.
- Otherwise returns the low or high
- Only in c++17 and later
- And some that would be very hard to implement.
- next_permutation
- Takes an two iterators.
- Could also take a comparison function.
- Assumes <
- Produces the next permutation in a sequence.
- And returns false if this is the last permutation.
- Normally starts with an ordered container.
- Or are just fun
- random_shuffle takes two iterators and randomly rearranges the items.
- reverse takes two iterators and reverses the elements in the container.
- See moreVector.cpp
- Try Mirror Images a kattis problem.
- See mirror.cpp