VOOZH about

URL: https://www.simplilearn.com/tutorials/cpp-tutorial/cpp-standard-template-library

⇱ Guide To Learn C++ STL (Standard Template Library) | Simplilearn


HomeResourcesSoftware DevelopmentC++ Tutorial for BeginnerUltimate Guide To Learn C++ STL (Standard Template Library)
Tutorial Playlist
C++ Tutorial for BeginnerOverview
The Easiest Guide to Understand and Learn C++ BasicsLesson - 1All You Need to Know About Classes in C++Lesson - 2The Difference Between C++ and CLesson - 3The Ultimate Guide to Learn About C++ EnumLesson - 4An Easy Guide To Understand The C++ ArrayLesson - 5The Best Guide to C++ For Loop : For Loops Made EasyLesson - 6Everything You Need to Know About C++ VectorLesson - 7The Easiest Way to Understand C++ FunctionsLesson - 8Call by Value and Call by Reference in C++Lesson - 9The Ultimate Step-by-Step Guide to Learn Pointers in C++Lesson - 10The Complete Guide to Learn the Difference Between Coding Vs ProgrammingLesson - 11C++ Object-Oriented Programming : The Best Way to Learn C++ OopsLesson - 12The Supreme Guide to Build the Tic Tac Toe Game in C++Lesson - 13The Best Guide to Create the C++ GUI ApplicationLesson - 14C++ vs. Python: Everything You Need to KnowLesson - 15What Is Sorting in C++: Bubble Sort, Insertion Sort & MoreLesson - 16The Best Explanation to Break and Continue Statements in C++Lesson - 17The Best Guide to Understand C++ Header FilesLesson - 18C++ Recursion: Understanding Its Concept and WorkingLesson - 19Top 60 C++ Interview Questions and Answers for 2026Lesson - 20C# Vs. Java: Which Is the Best Programming Language?Lesson - 21The Ultimate Guide to Learn C++ STL (Standard Template Library)Lesson - 22The Top 10 Reasons to Learn C++ Language in 2026Lesson - 23C++ Projects for Beginners : The Best Article for Beginner-Level ProjectsLesson - 24

Ultimate Guide To Learn C++ STL (Standard Template Library)

Lesson 22 of 24By Kartik Menon

Last updated on Jun 9, 202579451

Table of Contents

View More

C++ is a general-purpose and flexible programming language; that’s why you need a library to support the C++ language. C++ 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.

Want a Top Software Development Job? Start Here!AI-Powered Full Stack Developer ProgramExplore Program

Generic Programming

You can define generic programming as an approach that focuses on implementing, designing algorithms and data structures in a particular way such that there is no loss of efficiency. It is a way of developing software that maximizes code reuse without compromising performance. An example of generic programming is STL(standard template library) which is a part of the standard library of C++.

C++ STL

STL stands for Standard Template Library. Alexander Stepanov invented it in 1994, and later it was included in the standard library. The standard library consists of a set of algorithms and data structures that were originally part of the C++ Standard template library.

STL helps in storing and manipulating objects, and it makes the program reusable and robust.

Now, understand the components of STL.

Become an Automation Test Engineer in 11 Months!Automation Testing Masters ProgramExplore Program

Components of STL

STL has four major components:

  • Containers
  • Iterators
  • Algorithms
  • Function objects

So, start with containers.

Containers

If you are dealing with many elements, then you need to use some sort of container. The container can be described as objects that are used to store the collection of data. It helps in recreating and implementing complex data structures efficiently.

Now, containers are further classified into three categories:

πŸ‘ cpp_STL_Example1

  • Sequence containers: These are used to implement sequential data structures like a linked list, array, etc.
  • Associative containers: These are those containers in which each element has a value that is related to a key. They are used to implement sorted data structures, for example, set, multiset, map, etc.
  • Containers adapters: Container adapters can be defined as an interface used to provide functionality to the pre-existing containers.

Sequence Container:

  • Vectors: Vectors can be defined as a dynamic array or an array with some additional features.

Syntax:

πŸ‘ cpp_STL_Example2.

  • Deque: Deque is also known as a double-ended queue that allows inserting and deleting from both ends; they are more efficient than vectors in case of insertion and deletion.

Syntax:

πŸ‘ cpp_STL_Example3

  • List: List is also the sequential container and allows non-contiguous allocation. It allows insertion and deletion anywhere in the sequence.

Syntax:

πŸ‘ cpp_STL_Example4

Want a Top Software Development Job? Start Here!AI-Powered Full Stack Developer ProgramExplore Program

Associative Container:

  • Set: It is an associative container that is used to store elements that are unique.

Syntax:

πŸ‘ cpp_STL_Example5.

  • Multiset: This container is similar to that of the set container; the only difference is that it stores non-unique elements. 

Syntax:

πŸ‘ cpp_STL_Example6

  • Map: Map container contains sets of key-value pairs where each key is associated with one value.

Syntax:

πŸ‘ cpp_STL_Example7

Here, the int is the key type, and the int is the value type.

  • Multimap: These containers also store key-value pairs, but unlike maps, they can have duplicate elements.

Syntax:

πŸ‘ cpp_STL_Example8

Container Adapter:

  • Stack: Stack follows the last-in, first-out(LIFO) approach that means new elements are added at the end and removed from that end only.
  • Queue: Queue follows the first-in, first-out(FIFO) approach, which means new elements are added from the end and removed from the front.

Example:

πŸ‘ cpp_STL_Example9.

In the above example, you are using vector functions and some other functions to do some operations. After declaring the vector v, you add the elements inside the vector using the push_back() function and with the help of the loop. After that, you are displaying the size of the vector using the size() function. Now using the resize() function, you are resizing the vector size to 7. 

Using the empty() function, you check if the vector is empty; if it’s false, then you will display not empty, and if it is not false, then you will display the vector is empty. 

After that, you display all the vector elements using a for loop and functions like begin() and end(), pointing to the first element and the last element, respectively.

Learn 15+ In-Demand Tools and Skills!AI-Powered Automation Test Engineer ProgramExplore Program

Iterators 

Iterators are used to access data in the containers, and it helps in traversing through elements of containers using its functions. They can be incremented and decremented as per choice and help in the manipulation of data in the container.

Iterator functions are:

  • begin(): This function points the iterator to the first element of the container.
  • end(): This function points the iterator to the last element of the container.

Example:

πŸ‘ cpp_STL_Example10.

In the above example, you declare the vector using the initializer list and then declare iterator ptr to the vector.

After that, you declare the vector elements using the iterator functions begin(), and end() functions inside the for loop.  

Algorithms

In STL, different types of algorithms can be implemented with the help of iterators. Algorithms can be defined as functions applied to the containers and provide operation for the content of the container. for example : sort(), swap(), min(), max() etc.

Types of algorithms:

  • Modifying algorithms
  • Non-modifying algorithms
  • Sorting algorithms
  • Searching algorithms
  • Numeric algorithms

πŸ‘ cpp_STL_Example11

Example:

πŸ‘ cpp_STL_Example12.

In the above example, you have initialized the i and j variable with some values. And then, you have displayed those values; later, you have swapped those values using the swap() function.

Function Objects

A function object is also known as a functor; it is an object of a class that provides a definition for the operator(). Suppose you have declared an object of some class, then you can use that object just like the function, for example:

πŸ‘ cpp_STL_Example13

Here, you have class name C in which operator() is defined. Inside the main function, you have created the object obj and passed a string argument β€œWorld”. Here, you have used the object of the class instead of the function to pass the argument.

Conclusion

After reading this tutorial on C++ STL, you would have understood the Standard template library and generic programming and all the components of STL in C++, like containers, iterators, algorithms, in detail. You also learned about function objects along with some examples.

If you are perhaps looking to build a career in software development, do check the Post Graduate Program in Full Stack Development by Simplilearn. It can prove to be the ideal solution to help you build your career in the right way.

Do you have any questions regarding this tutorial on C++ STL? If you do, then please put them in the comments section. We’ll help you solve your queries. To learn more about C++ STL, click on the following link: C++ STL

About the Author

πŸ‘ Kartik Menon
Kartik Menon

Kartik is an experienced content strategist and an accomplished technology marketing specialist passionate about designing engaging user experiences with integrated marketing and communication solutions.

View More

Recommended Programs

πŸ‘ Full Stack Java Developer Masters Program

Full Stack Java Developer Masters Program

1029 Learners
Lifetime Access*
πŸ‘ AI-Powered Full Stack Developer Program

AI-Powered Full Stack Developer Program

1594 Learners
Lifetime Access*
πŸ‘ Java Certification Training

Java Certification Training

16365 Learners

*Lifetime access to high-quality, self-paced e-learning content.

Explore Category
  • Acknowledgement
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, OPM3 and the PMI ATP seal are the registered marks of the Project Management Institute, Inc.
  • *All trademarks are the property of their respective owners and their inclusion does not imply endorsement or affiliation.
  • Career Impact Results vary based on experience and numerous factors.