izzypt / CPP_Module_08

This module is designed to help you understand templated containers, iterators and algorithms in CPP.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CPP_Module_08

This module is designed to help you understand templated containers, iterators and algorithms in CPP.

Table of Content

Related links:

What is STL

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

STL Containers

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

image

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

image

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

image

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.

#include <iostream>
#include <vector>
using namespace std;

int main() {

  // initialize a vector of int type
  vector<int> numbers = {1, 100, 10, 70, 100};

  // print the vector
  cout << "Numbers are: ";
  for(auto &num: numbers) {
    cout << num << ", ";
  }

  return 0;
}

Output:

Numbers are: 1, 100, 10, 70, 100,

In the above example, we have created sequential container numbers using the vector class.

vector<int> 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.

// 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.

#include <iostream>
#include <set>
using namespace std;

int main() {

  // initialize a set of int type
  set<int> numbers = {1, 100, 10, 70, 100};

  // print the set
  cout << "Numbers are: ";
    for(auto &num: numbers) {
      cout << num << ", ";
    }

  return 0;
}

Output:

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:

set<int> 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.

// output numbers
1, 10, 70, 100

About

This module is designed to help you understand templated containers, iterators and algorithms in CPP.


Languages

Language:C++ 100.0%