Types Of Containers In C++ STL
Sequence containers
Sequential containers store container elements that can be accessed in sequence.
Types Of Sequential Containers:
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
-
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()
-
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!