Giter Club home page Giter Club logo

cpp_module_08's Introduction

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

cpp_module_08's People

Contributors

izzypt avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.