E N D
C++ • Containers Containers are based upon manipulating values rather than references (pointers) unless of course the values that are being manipulated are pointers. Containers copy or move elements in some ordered manner with the use of iterators by utilizing defined methods or operations. All container classes provide a constructor, a destructor, and a copy constructor. For instance, the vector class has use of the push_back method as well as the move method for copying data into the vector. • Initialization See Table 7.1 on page 255, The C++ Standard Library, second ed. (Josuttis). • Init with the elements of another container using the .begin() and .end() operation at declaration time. • Uniform initializer can be used in the form of a {, } list known as a an initlist. • Initialize a container from standard i/o using cin. • Assignment • ‘=‘ operator • move operator
C++ • Size operations • empty() – returns bool of whether the container is empty or not. • size() – returns the current number of elements in the container • max_size() – implementation specific but usually returns the highest value of the type of the index. • Comparisons • Utilizes the typical relational operators such as ‘==‘ or ‘!=‘. • When comparing containers, each must have the same type. • Containers are equal if and only if their elements are equal and have the same exact order. • Element Access • All containers give access via iterators. • Operators cbegin() and cend() provide read only access • Operators begin() and end() provide read/write access. • Operator clear() for vectors, deques, and strings make the begin() and end() operators invalid.
C++ • Arrays Models a static C-style array in the container class array<>. Fundamentally, it wraps the array class in a STL template. It is a sequence of elements that has a set size which is unchangeable – only the replacement of element value can be done. • In order to use the Array STL, the <array> header must be used. • Each declaration of the Array STL must include a type and size (e.g. <int,10>) • All elements of the array can be randomly accessed in no particular order. • Very good performance and usually allocated on the stack. • Initialization via uniform initialization • Arrays can be moved through the move semantics • Size operators include front() and back() • Array reference done through the data() operator.
C++ • Vectors A vector is a dynamic array – it has the ability to expand and contract but all elements must be homogenous. • In order to use the vector STL, the <vector> header must be used. • Each vector declaration must include a type (e.g. <int>) • It is similar to an array in that the elements can be accessed randomly. • Ability to reserve space up front as well as shrink the vector to fit what data is contained in the vector • If the .at() or [] is used to access elements in a vector, the index used must be valid. • Deques (Deck) A deque is a dynamic array and have many of the same properties as a vector except that both ends of the deque is open. Means Double Ended Queue. Vectors use a single array that needs to be occasionally reallocated for growth, the elements of a deque can be scattered in different chunks of storage, with the container keeping the necessary information internally to provide direct access to any of its elements in constant time and with a uniform sequential interface.
C++ • Insertion and removal from both ends of the structure • Provides random access iterators • Deques do not provide capacity functions • The push_back and pop_back methods access elements from the end • The push_front and pop_front methods access elements from the front. • Methods that manipulate the middle of the deque is relatively slow. • Lists Doubly linked list implementation. While the deque, vector, and array are based, fundamentally, on the same type of internal structure, the list is fairly different. There are two anchors which are pointers to the head and tail of the list, and each element points to the next and previous elements. • There is no random access in a list • Insertion and deletion is pretty quick and will not invalidate pointers to elements in close proximity
C++ Array Deque Vector
C++ Anchor List