Learn how the vector is one of the more common container types. It's random-access, bidirectional, and supports most of the common container functions.
- [Instructor] Containers are at the heart of the STL. They're it's reason for being. The other features of the STL mostly exist to support the containers. STL containers share a number of common features in order to make them convenient and easy to use. The point of STL containers is to make common data structures available to any compatible type or class. In doing this, the STL provides compatible interfaces for as many operations as possible.
Here I have a working copy of vector.cpp from chapter two of the exercise files. The vector is one of the more common STL containers. We'll use a vector to demonstrate some of the interface STL containers share in common. Vector is a sequence container. It holds objects in a strict sequential order. At the top here, you'll see we use the vector header. This makes all of the vector interface available to our program. We also have a template function for printing out the elements of a vector, and we have a simple message function with two different signatures for printing a simple message.
These functions are here to keep the rest of the code clean and free of the clutter of repeating patterns. The vector is constructed using normal template syntax. The type of the objects of the container is provided in the angle brackets here. This is a vector of ints and it's initialized using an initializer list. This should look familiar by now. There are several vector constructors available and we'll look at more of them below. In this case, we're using the initializer list.
This is available in C++ 11 and later. Let's go ahead and run this. You'll see vectors initialized from the initializer list. We use our printv to print out the elements of the vector. And we have some info member functions; size, front, and back. We see our size is 10; it has 10 elements. The front element has the value of 1 and the back element has the value of 10.
Most containers have a common set of member functions for accessing information for inserting or removing elements, for using iterators. Each container implements those functions that make sense for the particular type of container. Vector implements those functions that apply to a sequence container. And again, most of these functions apply to other sequence containers where they make sense. So the size, front and back, the indexes, getting the element at index number 5. We have two different ways of getting the index.
One is using the index operator from array and the other's using the app function. Here we have, at position number 5, at index number 5, is the value 6. So we can see that starts at 0. 0, 1, 2, 3, 4, 5. Element number 5 is the value 6. We can use insert to insert a value. In the case of insert, we're passing it an iterator.
We get the iterator at the begin point. We'll talk about iterators in a little more detail later. We get the begin iterator and we add 5 to it. That gives us the position to insert the value 42. So insert 42 at begin + 5, and we notice that now we have one more element in the container. It's the value 42 and it's at index 5. So it inserts it before the 6, because remember, 6 was the value at element number 5.
We can use erase with the same iterator, iterator + 5. So erase the begin point + 5 and now our 42 is gone. We can use push back, which pushes an element on the back of the vector. On the back of the vector here, we have a 47. We can pop back to remove an element from the back of the vector. Here we have removed the 47. There's a member function called empty which tests whether or not the vector is empty.
Here, we create a separate vector called vx with three elements in it. We loop through this vector testing the empty function. When empty is true, we're done. We can pop each of those elements off and printv. So we have 1, 2, 3; 1, 2; 1. So we're popping the back. When the vector is empty, our loop ends. Here, we're using insert with a slightly different syntax. We have vx.begin. Remember, vx was emptied in the last sequence.
So we're using insert to insert five elements from an initializer list. This is a very convenient way to use insert. You can see we print it here and it says 1, 2, 3, 4, 5, and our size is 5. And then we call clear, here it is, vx.clear, and our size is 0. We say printv and we get nothing because the vector is empty. Here's some more constructors. We can construct from a C-array.
Here's a C-array ia with a size of 10. We initialize by giving it the pointer at the beginning and the pointer at the end, or just past the end actually, of the array. Then we printv 2 here and our vector from the C-array has those 10 elements in it. We can fill a vector with one value. In this case, our vector is of type string and our value is the string string. We're putting five of them in, so this is a constructor that takes a number of how many times it's going to be repeated and the value to be repeated.
When we printv that one, it says string string string string string. We can use the copy constructor. Here, we're creating vector v4 from v3. And again it has that value with string string string string string. We can use the move constructor. So we have vector v5. We're using the move constructor with the standard move. For more about move semantics, see the companion course C++ Advanced Topics.
But this will move all of the elements from vector 4 to vector 5. Here, we're printing the size of vector 4 is 5 and then we do the move, and then the size of vector 4 is 0. Vector 5 has the 5 elements in it. Containers are at the heart of the STL. STL containers share a number of common features in order to make them convenient and easy to use. STL's container types provide compatible interfaces for as many operations as possible.
- Template fundamentals
- STL containers
- Accessing iterators
- Output, forward, and bidirectional iterators
- The transform function
- Transforming strings and types
- Arithmetic, relational, and logical functions
- STL algorithms