Table of contents
1.
Introduction 
2.
What are Containers?
3.
Types Of Containers In C++ STL
3.1.
Sequence containers
3.2.
Associative containers
3.3.
Unordered Associative containers
3.4.
Container Adapters
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Containers In C++ STL

Author Akash Nagpal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

The STL Container Library provides us with Containers, which are objects that contain data or a group of objects in the simplest terms. 

What are Containers?

A container is a storage object that contains a group of items (its elements).

Containers simplify constructing and duplicating complicated and specific data structures like arrays, lists, trees, and associative arrays.

Some common types of Containers widely used in programming are:

  • vector: it replicates arrays
  • queue: it replicates queues
  • stack: it replicates stack
  • priority_queue: it replicates heaps
  • list: it replicates linked list
  • set: it replicates trees
  • map: it associative arrays

Types Of Containers In C++ STL

Sequence containers

Sequential containers store container elements that can be accessed in sequence.
Types Of Sequential Containers: 

  • Array
  • Vector
  • Deque
  • List

 

Example Code:

#include <iostream>
#include <vector>
using namespace std;
int main() {
	// initializing a vector
	vector<int> numbers = {1, 100, 10, 70, 100};
	// printing the elements
	cout << "Numbers are: ";
	for(auto &num: numbers) {
		cout << num << ", ";
	}
	return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output

Numbers are: 1, 100, 10, 70, 100,
You can also try this code with Online C++ Compiler
Run Code

 

Associative containers

We can store elements in sorted order using associative containers. This sorted order is independent of when the ingredient is added and internally, these are implemented using binary tree data structures.

Types Of Associative Containers: 

  • Set
  • Map
  • Multiset
  • Multimap

 

Example Code:

#include <iostream>
#include <set>
using namespace std;
int main() {
	// initializing the set with int
	set<int> num1 = {1, 100, 10, 70, 100};
	// print the set
	cout << "Numbers are: ";
	for(auto &num: num1) {
		cout << num << ", ";
	}
	return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output

Numbers are: 1, 10, 70, 100,
You can also try this code with Online C++ Compiler
Run Code

 

Unordered Associative containers

These are the unsorted version of Associative Containers, and internally these are implemented by hash table data structures.
Types of Unordered Associative Containers: 

  • Unordered Set
  • Unordered Map
  • Unordered Multiset
  • Unordered Multimap
     

Example Code:

#include <iostream>
#include <unordered_set>
using namespace std;
int main() {
// initializing unordered_set
unordered_set<int> numbers = {1, 100, 10, 70, 100};
// printing the unordered set
cout << "Numbers are: ";
for(auto &num: numbers) {
cout << num << ", ";
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output

Numbers are: 70, 10, 100, 1,
You can also try this code with Online C++ Compiler
Run Code

 

Container Adapters

Each of these "container adaptors" is built by "adapting" (restricting/modifying) the interface of one of the sequential containers listed above to achieve the necessary function.

Types Of Container Adapters are: 

  • Stack
  • Queue
  • Priority Queue

 

Example Code:


#include <iostream>
#include <stack>
using namespace std;
int main() {
	// creating a stack
	stack<int> numbers;
	// push into stack
	numbers.push(1);
	numbers.push(100);
	numbers.push(10);
	cout << "Numbers are: ";
	while(!numbers.empty()) {
		// print top element
		cout << numbers.top() << ", ";
		// pop top element from stack
		numbers.pop();
	}
	return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output

Numbers are: 10, 100, 1,
You can also try this code with Online C++ Compiler
Run Code

FAQs

  1. What is Priority Queue in C++ STL? Mention some of the methods of Priority Queues.
    Priority queues are a sort of container adapter. The first queue element is the biggest of all the items in the queue, and the elements are in non-increasing order.
    Priority Queues includes various methods like priority_queue::empty(), priority_queue::size(), priority_queue::pop(), priority_queue::swap()
  2. What are Lists in C++ STL?
    In C++, a List is another significant Container. It supports bidirectional, linear lists and implements deletion and insertion operations efficiently.

Key Takeaways

In this article we have extensively discussed the C++ STL Containers and their types. Check out the Algorithms in C++ STL for further topics.

We hope that this blog has helped you enhance your knowledge regarding C++ STL Containers and if you would like to learn more, check out our articles on Coding Ninjas and visit our Library for more. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass