BLUF
C++ containers are a set of data structures that are used to store and organize collections of data. There are several different types of containers available in C++, each with its own set of features and characteristics.
Here are some common C++ containers:
Arrays: A fixed-size container that can store a collection of elements of the same data type in contiguous memory.
Vectors: A dynamic array that can grow or shrink as elements are added or removed. Vectors are similar to arrays but provide additional functionality, such as the ability to resize the container dynamically.
Lists: A container that stores elements in a linked list. Lists are efficient for inserting or removing elements at any position, but accessing elements is slower than with arrays or vectors.
Maps: A container that stores key-value pairs in a sorted order. Maps are efficient for searching and retrieving data based on a key.
Sets: A container that stores unique elements in a sorted order. Sets are useful when you need to maintain a collection of elements without duplicates.
Queues: A container that stores elements in a FIFO (first-in, first-out) order. Queues are useful when you need to process elements in the order they were added.
Deque: (Double Ended Queue) is a sequence container with dynamic sizes that can be expanded or contracted on both ends. It provides similar functionality to vector, but it also provides functionality for constant time insertion and deletion of elements at the beginning and end of the sequence.
Stacks: A container that stores elements in a LIFO (last-in, first-out) order. Stacks are useful when you need to process elements in reverse order.
Each of these containers has its own set of methods and functions that can be used to manipulate the data it stores. For example, you can add elements to a vector using the push_back
method, or remove elements from a list using the erase
method. Choosing the right container for your needs depends on the specific requirements of your program, such as the type of data being stored, the operations that need to be performed, and the expected size and complexity of the container.
Lists: Lists are especially useful when you need to perform frequent insertions and deletions in the middle of the sequence, and you do not need to access elements by their position. For example, you might use a list in a system for managing a to-do list, where tasks can often be inserted or removed in the middle of the list, and there's typically no need to access tasks by their position.
Pair: Pairs are great for associating two items together that might not naturally fit into a class or structure. For instance, a pair could be useful if you wanted to keep track of a student's name and their corresponding grade.
Map: Maps are commonly used when there is a key-value relationship between elements and you need to frequently look up values by their corresponding keys. For instance, you could use a map to associate students' names with their grades in a gradebook application, where you need to quickly find a student's grade by their name.
Set: Sets are helpful when you need to maintain a collection of elements, but you care more about whether or not an item is in the collection than about its order or position. Sets ensure that each element is unique and allows for efficient lookup of elements. For instance, you might use a set to represent a group of students in a class and check whether a particular student is in the class.
Queue: Queues are useful when you need to maintain a FIFO (First In First Out) order of elements. They're used in scenarios like CPU scheduling, Disk Scheduling, and more. A real-life example could be a print queue, where print tasks are removed for printing in the order they were added (i.e., the first document added is the first one to be printed).
Deque: Deques are useful when you need to frequently add or remove elements from both ends of the sequence, such as in certain specific algorithms like the 'sliding window' algorithm. The random access operation (accessing an element at a certain index) is also efficient in a deque, so if your problem involves such operations, a deque would be a better choice than a list or a forward_list.
\uD83D\uDCD8 Outline
\uD83D\uDCCB To Do
Containers Active Reading
Review Student Notes
Complete Lab 1 and 2