C++ STL

C++ Programming Examples - STL

STL stands for Standard Template Library. STL is a collection of C++ template classes for common programming data structures and functions such as lists, stacks, and arrays. It is a container library with algorithms, iterators, and container classes. STL helps to store and manipulate objects and makes the program reusable and robust. Using the STL in C++ makes the code more expressive, simpler, and easier to understand.

STL Components:

  • Algorithms
  • Containers
  • Iterators
  • Functions

Algorithms

Algorithms are functions that are applied to the container and provide an operation for the container’s contents. In STL, different types of algorithms can be implemented using iterators.

Types of Algorithms in STL Library:

  • Searching Algorithms
  • Sorting Algorithms
  • Numeric Algorithms
  • Modifying Algorithms
  • Non Modifying Algorithms

Containers

advertisement
advertisement

Containers are objects that are used to store information. It helps the effective creation and execution of complicated data structures.

Classification of STL Containers:

  • Sequence Containers – Arrays, Vector, List, Dequeue
  • Associative Containers – Set, multiset, Map, Multimap
  • Unordered Associative Containers – unordered_set, unordered_multiset, unordered_map, unordered_multimap
  • Containers Adaptors – Stack, Queue, priority_queue

Iterators

Iterators are used to point to memory addresses in the STL container. It is mainly used in sequences of numbers, characters, etc. The most commonly used iterator operations are begin(), end(), advance(), next(), and, prev().

Types of Iterators:

  • Input Iterators
  • Output Iterators
  • Random-Access Iterators
  • Forward Iterator
  • Bidirectional Iterators

Functions

The STL includes classes that overload the function call operator (). Instances of these classes are called functors or function objects.

The following section contains various C++ programs on standard template library, sequence containers, container adaptors, associative containers, and unordered associative containers. Each sample program includes a program description, C++ code, and program output. All examples have been compiled and tested on Windows and Linux systems.

advertisement

Here is the listing of C++ programming examples on STL:

  1. C++ Programs on STL Sequence Containers
  2. C++ Programs on STL Container Adaptors
  3. C++ Programs on STL Associative Containers
  4. C++ Programs on STL Unordered Associative Containers
  5. C++ Programs on Standard Template Library

1. C++ Programs on STL Sequence Containers

Program Description
Vector Container in C++ C++ Program to Illustrate Usage of Vector Container
Deque in C++ C++ Program to Illustrate Usage of Deque
List in C++ Program C++ Program to Illustrate Usage of List
Array in C++ STL C++ Program to Implement Array in STL
Vector in C++ STL C++ Program to Implement Vector in STL
Deque in C++ STL C++ Program to Implement Deque in STL
List in C++ STL C++ Program to Implement List in STL
Forward_List in C++ STL C++ Program to Implement Forward_List in STL

2. C++ Programs on STL Container Adaptors

Program Description
Stack Adaptor in C++ Program C++ Program to Illustrate usage of Stack Adaptor
Queue Container in C++ C++ Program to Illustrate Usage of Queue
Stack in C++ STL C++ Program to Implement Stack in STL
Queue in C++ STL C++ Program to Implement Queue in STL
Priority_queue in C++ STL C++ Program to Implement Priority_queue in STL

advertisement

3. C++ Programs on STL Associative Containers

Program Description
Map Container in C++ Program C++ Program to Illustrate Usage of Map Container
Bitset in C++ Program C++ Program to Illustrate Usage of Bitset
Set in C++ STL C++ Program to Implement Set in STL
Set_Difference in C++ STL C++ Program to Implement Set_Difference in STL
Set_Intersection in C++ STL C++ Program to Implement Set_Intersection in STL
Set_Symmetric_difference in C++ STL C++ Program to Implement Set_Symmetric_difference in STL
Set_Union in C++ STL C++ Program to Implement Set_Union in STL
Multiset in C++ STL C++ Program to Implement Multiset in STL
Map in C++ STL C++ Program to Implement Map in STL
Multimap in C++ STL C++ Program to Implement Multimap in STL

4. C++ Programs on STL Unordered Associative Containers

Program Description
Unordered_set in C++ STL C++ Program to Implement Unordered_set in STL
Unordered_multiset in C++ STL C++ Program to Implement Unordered_multiset in STL

5. C++ Programs on Standard Template Library

Program Description
String Streams in C++ C++ Program to Illustrate Usage of String Streams
Struct in C++ Program C++ Program to Illustrate Usage of Structs
String in C++ STL C++ Program to Implement String in STL
LexicoGraphical_Compare in C++ STL C++ Program to Implement LexicoGraphical_Compare in STL
Pairs in C++ STL C++ Program to Implement Pairs in STL
Prev_Permutation in C++ STL C++ Program to Implement Prev_Permutation in STL
Next_Permutation in C++ STL C++ Program to Implement Next_Permutation in STL
Sorting Containers in C++ STL C++ Program to Implement Sorting Containers in STL

advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.