https://github.com/izzypt/cpp_module_08
This module is designed to help you understand templated containers, iterators and algorithms in CPP.
https://github.com/izzypt/cpp_module_08
Last synced: about 2 months ago
JSON representation
This module is designed to help you understand templated containers, iterators and algorithms in CPP.
- Host: GitHub
- URL: https://github.com/izzypt/cpp_module_08
- Owner: izzypt
- Created: 2023-12-12T13:48:40.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-14T21:34:29.000Z (over 2 years ago)
- Last Synced: 2025-01-14T21:46:38.446Z (over 1 year ago)
- Language: C++
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CPP_Module_08
This module is designed to help you understand templated containers, iterators and algorithms in CPP.
# Table of Content
- [What is STL](#stl)
- [What are STL containers](#containers)
- [Types of STL Container in C++](#types)
- [Sequential Containers](#sequential)
- [Associative Containers](#associative)
- [Unordered Associative Containers](#unordered)
- [Sequential Exampple](#sequential_example)
- [Associative Example](#associative_example)
- [Unordered Exampple](#unordered_example)
### Related links:
- https://www.scaler.com/topics/cpp/containers-in-cpp/
STL (standard template library) is a software library for the C++ language that provides a collection of templates representing containers, iterators, algorithms, and function objects. In this tutorial, you will learn about C++ STL in detail.
STL has 4 major components:
- Algorithms
- Containers
- Functors
- Iterators
A Standard Template Library (STL) container in C++ is a template class or type that provides a way to store and organize elements of the same or different types.
These containers implement various data structures and algorithms for common tasks, offering a high level of abstraction and providing a consistent interface.
STL containers play a crucial role in C++ programming by offering efficient and reusable solutions for managing collections of data.
Containers abstract away the underlying data structure and implementation details, allowing programmers to focus on algorithms and logic without worrying about low-level details.
# Types of STL Container in C++
In C++, there are generally 3 kinds of STL containers:
- **Sequential Containers**
- **Associative Containers**
- **Unordered Associative Containers**

# Sequential Containers in C++
In C++, sequential containers allow us to store elements that can be accessed in sequential order.
Internally, sequential containers are implemented as arrays or linked lists data structures.
Types of Sequential Containers
- **Array**
- **Vector**
- **Deque**
- **List**
- **Forward List**

# Associative Containers in C++
In C++, associative containers allow us to store elements in sorted order. The order doesn't depend upon when the element is inserted.
Internally, they are implemented as binary tree data structures.
Types of Associative Containers
- **Set**
- **Map**
- **Multiset**
- **Multimap**

# Unordered Associative Containers in C++
In C++, STL Unordered Associative Containers provide the unsorted versions of the associative container.
Internally, unordered associative containers are implemented as hash table data structures.
Types of Unordered Associative Containers
- **Unordered Set**
- **Unordered Map**
- **Unordered Multiset**
- **Unordered Multimap**
# Sequential Container Example
In this example, we will be using the vector class to demonstrate the working of a sequential container.
```cpp
#include
#include
using namespace std;
int main() {
// initialize a vector of int type
vector numbers = {1, 100, 10, 70, 100};
// print the vector
cout << "Numbers are: ";
for(auto &num: numbers) {
cout << num << ", ";
}
return 0;
}
```
Output:
```cpp
Numbers are: 1, 100, 10, 70, 100,
```
In the above example, we have created sequential container numbers using the vector class.
```cpp
vector numbers = {1, 100, 10, 70, 100};
```
Here, we have used a ranged for loop to print each element of the container.
In the output, we can see the numbers are shown in sequential order as they were initialized.
```cpp
// output numbers
1, 100, 10, 70, 100,
```
# Associative Container Example (set)
In this example, we will be using the set class to demonstrate the working of an associative container.
```cpp
#include
#include
using namespace std;
int main() {
// initialize a set of int type
set numbers = {1, 100, 10, 70, 100};
// print the set
cout << "Numbers are: ";
for(auto &num: numbers) {
cout << num << ", ";
}
return 0;
}
```
Output:
```cpp
Numbers are: 1, 10, 70, 100,
```
In the above example, we have created an associative container using the set class.
We have initialized a set named numbers with unordered integers, along with a duplicate value 100:
```cpp
set numbers = {1, 100, 10, 70, 100};
```
Then we print the content of the set using a ranged for loop.
In the output, we see that the numbers are sorted in ascending order with duplicate numbers removed. Initially, 100 was repeated twice but the set removes the duplicate number 100.
```cpp
// output numbers
1, 10, 70, 100
```