1 / 10

C++

C++. Containers

quiana
Download Presentation

C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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.

  3. 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.

  4. 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.

  5. 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

  6. C++ Array Deque Vector

  7. C++ Anchor List

  8. C++ • Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually, the sort implementation is a binary tree which implies that elements cannot be changed directly. Thus, an element has to be deleted, modified, and then re-inserted. • Made up of a key or search criteria. • Use the insert() method in assigning elements, and erase() method for deletion. Also, emplace() will copy an element with the arguments passed in and return the position. • Associative container based upon sorting of the element values. • Ability to have a default sorting operation when declared. • Use the #include <set> directive to pick up set support.

  9. C++ • Maps and Multimaps Where sets sort elements based upon the association of the value of the element itself, maps and multimaps sort elements based upon some key in the element associated with the value of the element. Multimaps allow duplicate key/value pairs where as maps do not. • The key and value of a map or multimap must be copyable/movable. • The key must be comparable based upon the sorting criteria. • Usually binary tree based sorting. • Like sets, the key cannot be changed directly due to the side effect of compromising the order. • The element must be removed, modified, and then re-inserted as is the case with a set.

  10. C++ Sets Map

More Related