Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Types of containers
2.1.
List of sequence containers
2.2.
List of associative containers
2.3.
List of unordered associative containers
2.4.
List of container adapters
3.
Frequently asked questions
4.
Key Takeaways
Last Updated: Mar 27, 2024

STL containers in C++

Author Shreya Deep
2 upvotes
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

We all know what a container means in the English language. In the English language, a container is a box or a packet that can store something. Similarly, in the C++ language, we have containers used to store a collection of objects. We can access the elements stored in a container directly by their assigned indexes or iterators.

Types of containers

There are majorly four types of containers that we are going to learn, namely, sequence containers, associative containers, unordered associative containers, and container adaptors. 

  • Sequence containers are containers in which elements are stored sequentially.
     
  • Associative containers are containers in which elements are not stored sequentially. Instead, they are stored at random locations, always in sorted order. Since elements are in sorted order, it takes only O(logn) time to search an element. 
     
  • Unordered associative containers are associative containers in which elements are unordered, i.e., unsorted.
     
  • Container adapters are containers made in some other container and limit that container to certain functionalities. For example, a stack is a container adapter. It can be implemented using an array or a deque, and we can push and pop elements only from one side i.e. top of stack.

List of sequence containers

  • Array - Arrays are static containers in which stores the elements contiguously. 
     
  • Vector - Vectors are dynamic arrays, and just like arrays it also stores the elements in a contiguous manner.
     
  • Deque - A deque is a double-ended queue. So, we can call it a dynamic container, and it has the property that elements can be popped and pushed into it from both front and back sides.
     
  • Linked list - A linked list is a linear data structure that consists of nodes. Each Node contains a data field and a pointer to the next Node. In Linked List, unlike arrays, elements are not stored at contiguous memory locations but rather at different memory locations.
     
  • Doubly linked list - Doubly linked lists are a type of linked lists that can be traversed in both directions, i.e., forward (head to the last Node) and backward direction (last Node to head). 

List of associative containers

  • Set - Sets are a type of data structure that can store all the unique values. Elements in a set are sorted in ascending order of key values.
     
  • Map - Map is a data structure that stores unique key-value pairs. The items in a map are sorted in ascending order of key values. Also, we can search the value using the corresponding key because keys are unique.
     
  • Multiset - Multiset is very similar to the set data structure. The only difference is that the multiset's values need not be unique.
     
  • Multimap - Multimap is very similar to the map data structure. The only difference is that the values corresponding to a key can be more than one in multimap.

List of unordered associative containers

  • unordered_set - Unordered sets are a type of data structure that can store all the unique values. Elements in a set are not sorted.
     
  • unordered_map - Unordered map is a data structure that stores key-value pairs in it. We can search the value using the corresponding key because keys are unique.
     
  • unordered_multiset - unordered multiset is a data structure very similar to the unordered set data structure. The only difference is that the values need not be unique in an unordered multiset.
     
  • unordered_multimap - an unordered multimap is very similar to the unordered map data structure. The only difference is that the values corresponding to a key can be more than one in an unordered multimap.

List of container adapters

  • Stack - Stack is an abstract data type (ADT) that follows the Last in First Out (LIFO) principle. This means that the element which is added last in the stack will be accessed first. Inserting an element in the stack is called PUSH operation; removing an element in the stack is called POP operation.
     
  • Queue - Queue is a data structure that follows the First in First Out (FIFO) principle. This means that the element added first in the queue will be accessed first.
     
  • Priority Queue - Priority Queue is a data structure that stores elements according to their priority. Unlike a normal queue, the element with the highest priority is always at the front of the queue. If that element is removed, the element with the next highest priority moves to the front. By default, a priority queue arranges elements in ascending order.
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently asked questions

  1. Are containers and data structures the same?
    No, containers are just a subset of data structures. There are many data structures out there, but containers are a few.
     
  2. Which containers have items sorted in ascending order?
    The ordered associative containers have items sorted in ascending order in them. The containers having only values as items are sorted by values and the containers having key-value pairs have items sorted by key.
     
  3. What are the containers that are constructed over another container?
    Container adapters are constructed over another container. For example, a stack is formed using an array or a deque.
     
  4. Which containers are used to implement a priority queue?
    A binary heap is used to implement a priority queue. The highest priority element is placed on the top of the heap, i.e., on the root.
     
  5. What is the difference between set and multiset?
    A set contains only unique values, whereas a multiset can also contain duplicate values.

Key Takeaways

In this article, we discussed the containers in C++ and their properties. Containers are a subset of data structures. To learn more about other data structures, head over to this. Are you planning to ace the interviews of reputed product-based companies like Amazon, Google, Microsoft, and more? 

Recommended Readings:

 

Attempt our Online Mock Test Series on Coding Ninjas Studio now!

Also check out - Inorder Predecessor

Happy Coding!

Next article
Containers In C++ STL
Live masterclass