C Programming and Data Structures (16 Blogs)

STL in c++ : Everything you Need to Know

Published on Aug 07,2019 6.2K Views


The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. In this STL in C++ article we will discuss the following pointers:

Moving on with this article on STL in C++

C++ Templates

C++ provides us with a feature of templates that allows functions and classes to operate with generic types. This allows the reusability of a function or class and allows it to work on many different data types without being rewritten for each one.

Moving on with this article on STL in C++

Generic functions and STL

While programming many a time there is a need for creating functions that perform the same operations but work with different data types. So to overcome this problem C++ provides a feature to create a single generic function instead of many functions which can work with different data type by using the template parameter. The collection of these genric classes and functions is called Standard Template Library(STL)

The components of STL which are now part of the standard C++ library are defined in the namespace std. We must, therefore, use the using namespace directive to import them into our program.

Syntax:

Using namespace std;

STL has three components

  • Containers

  • Algorithms

  • Iterators

These three components work together with one another in synergy to provide support to a variety of programming solutions. Algorithm employ iterators to perform operation stored in containers.

A container is an object that stores data in memory into an organized fashion. The containers in STL are implemented by template classes and therefore can be easily modified and customized to hold different types of data.

A procedure that is used to process the data contained in the containers is defined as an algorithm. The STL includes many different kinds of algorithms to provide support to tasks such as initializing, searching, copying, sorting, and merging, copying, sorting, and merging. Algorithms are implemented by template functions.

An iterator can be defined as an object that points to an element in a container. Iterators can be used to move through the contents of containers. Iterators are handled just like pointers. We can increment or decrement them. Iterators connect algorithm with containers and play a key role in the manipulation of data stored in the containers.

Container-STL-in-C++

Moving on with this article on STL in C++

Containers

STL defines ten containers which are grouped into three categories.

Categories-STL-in-C++

Containers

Description

Header file

Iterator

Vector

It can be defined as a dynamic array. It permits direct access to any element.

<vector>

Random access

List

It is a  bidirectional linear list. It allows insertion and deletion anywhere

<list>

Bidirectional

deque

It is a double-ended queue. Allows insertions and deletions at both the ends. Permits direct access to any element.

<deque>

Random access

set

It is an associate container for storing unique sets. Allows rapid lookup.

<set>

Bidirectional

multiset

It is an associate container for storing non-unique sets.

<set>

Bidirectional

map

It is an associate container for storing unique key/value pairs. Each key is associated with only one value.

<map>

Bidirectional

multimap

It is an associate container for storing key/value in which one key may be associated with more than one value (one-to-many mapping). It allows a key-based lookup.

<map>

Bidirectional

stack

A standard stack follows last-in-first-out(LIFO)

<stack>

No iterator

queue

A standard queue follows first-in-first-out(FIFO)

<queue>

No iterator

priority-queue

The first element out is always the highest priority element

<queue>

No iterator

Sequence Containers

Sequence containers store elements in a linear order. All elements are related to each other by their position along the line. They allow insertion of element and all of them support several operations on them.

Sequence-Containers-STL-in-C++

The STL provides three types of sequence elements:

  • Vector
  • List
  • Deque

Associative containers:

They are designed in such a way that they can support direct access to elements using keys. They are not sequential. There are four types of

associative containers:

  1. Set
  2. Multiset
  3. Map
  4. Multimap

All the above containers store data in a structure called tree which facilitates fast

searching, deletion, and insertion unlike sequential. Container set or multiset can store various items and provide operations for manipulating them using the values as the keys.

And map or Multimap are used to store items in pair, one called the key and other

called the value.

Derived containers:

The STL provides three derived containers namely, stack, queue, and priority_queue. These are also known as container adaptors.

There are three types of derived containers:

1.Stack

2.Queue

3.Priority_quue

Stacks, queue and priority queue can easily be created from different sequence containers. The derived containers do not support iterators and therefore we cannot use them for data manipulation. However, they support two member function pop() and push() for implementing deleting and inserting operations.

Moving on with this article on STL in C++

Algorithms

Algorithms are functions that can be used generally across a variety of containers for processing their content. Although each container provides functions for its basic operations, STL provides more than sixty standard algorithms to support more extended or complex operations. Standard algorithms also permit us to work with two different types of containers at the same time.

STL algorithms reinforce the philosophy of reusability. By using these algorithms, programmers can save a lot of time and effort. To have access to the STL algorithms, we must include <algorithm> in our program.

STL algorithm, based on the nature of operations they perform, may be categorized as under :

  • Nonmutating algorithms

  • Mutating algorithms

  • Sorting algorithms

  • Set algorithms

  • Relational algorithm

Moving on with this article on STL in C++

Iterators:

Iterators act like pointers and are used to access elements of the container. We use iterators to move through the contents of containers. Iterators are handled just like pointers. We can increment or decrement them as per our requirements. Iterators connect containers with algorithms and play a vital role in the manipulation of data stored in the containers. They are often used to pass through from one element to another, this process is called iterating through the container.

There are five types of iterators:

1.Input

2.Output

3.Forward

4.Bidirectional

5.Random

Iterator

Access method

Direction of movement

I/O capability

Remark

Input

Linear

Forward only

Read-only

Cannot be saved

Output

Linear

Forward only

Write only

Cannot be saved

Forward

Linear

Forward only

Read/Write

Can be saved

Bidirectional

Linear

Forward and backward

Read/Write

Can be saved

Random

Random

Forward and backward

Read/Write

Can be saved

Different types of iterators must be used with the different types of containers such that only

sequence and associative containers are allowed to travel through iterators. Each type of iterators is used for performing certain functions. The input and output iterators support the least functions. They can be used only to pass through in a container. The forward iterators support all operations of input and output iterators and also retain its position in the container. A Bidirectional iterator, while supporting all forward iterators operations, provides the ability to move in the backward direction in the container.

Thus we have come to an end of this article on ‘STL in C++’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

STL in c++ : Everything you Need to Know

edureka.co