Sequence and Associative Containers in C++A Container in C++ is an object that stores collections of additional information. These packages can hold any data type, particularly user-defined data types, because they are implemented as class templates. In C++, there are three kinds of containers: sequential containers, associative containers, and unorganized (associative) containers. Sequential ContainersAn ordered group of the same kind of data in C++, where each element is kept at a designated location, is called a sequential container. The position of an element is determined by the location and timing of its insertion. Sequential containers are sometimes referred to as sequence containers. There are 3 types of sequential containers: 1. Arrays:Arrays are containers with a defined size. They have a limited number of components. These components are placed in a tight linear order. We can use array items directly by giving their index, so we don't have to traverse the entire array. Example: Let's take a C++ example to illustrate the use of array in sequence containers: Output: The array is: 11 16 19 10 14 The Size of the array given array is: 5 Explanation: In this program, we made use of the array standard library to build an array a. After that, we used many array library specified functions. The methods begin() and end() return the pointer to the array's beginning and last members. The size() function returns the size of the array. 2. VectorsThe vectors are array containers that may change in size. Vectors, like arrays, employ contiguous regions of memory to hold their items. However, unlike arrays, the size of a vector's data can change constantly, and storage is handled effectively by the container that holds it. Example: Let's take a C++ example to illustrate the use of Vectors in sequence containers: Output: The output with vect.begin() and vect.end(): 1 2 4 8 16 32 The output with vect.rbegin() and vect.rend(): 32 16 8 4 2 1 Explanation: In this example, we constructed a vector vect. We added components to this vector using the push_back() method. After that, we made use of the begin() and end() operations, as well as the rbegin() and rend() functions, to generate a print of the vector from left to right and right to left. 3. Deque:Deque is an abbreviation for double-ended queue. A deque is a container with dimensions that can change dynamically on both ends. We may access individual elements of a Deque using random access iterators (generalization pointers that can read, write, and provide random access to containers). Example: Let's take a C++ example to illustrate the use of Deque in sequence containers: Output: The dequeue is: 40 20 10 30 The size deq.size(): 4 The Deque after using pop_front() is: 20 10 30 Associative containersAssociative containers are ones in which the position of an element is determined by its value. The sequence in which items are inserted has no impact on the element's location. The keys, sometimes known as search keys, are used to access the components in an associative container. There are 4 different kinds of associative containers: 1. SetA set is a container that requires each element (or key) to be unique. Once an element is added to a set, it cannot be changed. However, the element can be deleted from the set. A set's elements are kept in a sorted manner to allow for easy storage and retrieval. Example: Let's take a C++ example to illustrate the use of Set in Associative containers: Output: The set ss1 is -> 50 70 80 90 Set ss1 after the using of s1.erase(50) method-> 70 80 90 Explanation: In this example, we tried to put the number 50 twice into the set. However, 50 have been added to the set once because every component in a set must be unique. After that, the set element was removed using the erase() method. 2. MultisetMultisets are containers that are identical to sets but accept duplicate items (keys). The value of a single element in a multiset cannot be changed after it has been saved, although the element may be removed from the set's container. Example: Let's take a C++ example to illustrate the use of Multiset in Associative containers: Output: The multiset ms1 is: 30 30 40 60 80 The multiset after the removal of values< 40: 40 60 80 Explanation: In this example, we were able to add element 30 twice in the given application because multisets enable element duplication. We additionally utilized the erase() method to delete all elements in the multiset that were less than 40. The wipe function could have erased the whole multiset if 40 had not been included in the set. 3. MapA Map is a container that holds data in the form of key-value pairs. A map container's variables must be unique, and each key must have only one value attached to it. The data type that makes up the key and the value associated with it may differ. A map's components are kept in relation to the key. Any element can be added or removed. Example: Let's take a C++ example to illustrate the use of Map in Associative containers: Output: The map map1 contains: KEY -> VALUE 1 -> c 2 -> k 3 -> h 4 -> i 5 -> c After running m1.erase(4), the map is: KEY -> VALUE 1 -> c 2 -> k 3 -> h 5 -> c Explanation: In this example, we created a map and used the insert() method to add entries to it. After that, a component with key = 4 was removed using the erase() method. 4. MultimapMultimap containers have similarities to map containers, except they enable Multiple keys to be stored in a container. As a result, the key-value pairs have a one-to-many connection. In multimaps, the key's datatype might differ from its corresponding value's. Example: Let's take a C++ example to illustrate the use of Multimap in Associative containers: Output: The multimap m1 ha elements: KEY -> VALUE 31 -> 3 34 -> 1 35 -> 4 61 -> 6 90 -> 9 The multimap mmap1 after removing the key values up to (31 KEY -> VALUE 31 -> 3 34 -> 1 35 -> 4 61 -> 6 90 -> 9 Explanation: In this example, we constructed a multimap. After that, we can use the same key twice in the multimap because it enables duplicate key values. We used the erase() method to eliminate all entries with key numbers less than 31. Main Differences between Sequence and Associative Containers in C++:These are the differences between Sequence and Associative containers in C++:
Next Topicstd::get_temporary_buffer in C++ |