std::
namespace.#include <vector>
, and then we can use the vector name;
syntax to declare a vector (as usual, any. It's always good practice to know how to break things and what to expect when things do go wrong, and as such you should always be testing all of the rules and boundaries that I present to you.push_back
and pop_back
member functions respectively, take for example the following:.back
member function, and the first element with the .front
member function, vectors can be resized using the resize
member function, the size of a vector can be retrieved using the size
member function... the list goes on. If you want to know more of the member functions that can be used on vectors, I suggest the relevant cplusplus.com reference page.insert
member function with two parameters. Note, however, that the insert
member function is pretty damn inefficient memory-wise, and if you're going to be regularly inserting data into the vector, you may want to look for another container. Vectors are good at defining and accessing elements through the whole container, but they're especially good at working with the elements at the end of the vector (pushing, popping, and so forth). This is why a good knowledge of the different containers is useful, as some containers are more fit for certain jobs than others (and hence, in future tutorials we will be covering some different containers).capacity
member function - and as such you can see the points at which the vector switches to a bigger array by using a simple loop:#include <vector>
, we also get these great little things called vector iterators bundled along.::iterator
stuck on the end -- for example:.begin()
and .end()
vector methods returning references to the first and last elements respectively:.erase
is a useful example of this which allows for the removal of an element in the vector via an iterator parameter of the element you want to remove.