CHAPTER 18  

STANDARD TEMPLATE LIBRARY

 

WHY STL (STANDARD TEMPLATE LIBRARY)? 

There are common data structures and algorithms that are used over and over in programs. C++ provides ready-made solutions for these data structures and algorithms by including them in a library. Therefore, a programmer can simply use them rather than make them. The advantage is that the programmer can use these data structures and algorithms that are defined, standardized, tested, and ready to be used rather than starting from scratch each time.  In addition to the original built-in functions of C/C++, in 1994 STL became part of the standard library by adding new classes that work with any data type, promoting reuse rather than reinventing the wheel. Certain data structures and algorithms are used over and over by programmers, so why not automate these data structures and algorithms? Certain components of STL are better suited for different circumstances. 

For beginners or even experienced programmers STL can be difficult to grasp. The syntax of STL is a little intimidating.  After observing the capabilities of STL, the value of this tool becomes obvious and you want to use it more. Just keep in mind that the goal of STL is to provide you with a collection of generic tools so that you can enhance your programmability. If you have had difficulty building a linked list and operating it manually, using STL you can request an automatic double linked list with the all the necessary functions to operate it. Just remember STL by itself is a vast topic and you need time to become acquainted with it. 

THREE COMPONENTS OF STL: CONTAINERS, ALGORITMS, AND ITERATORS 

STL comes with three components that work with each other to solve problems of all different kinds. These three components are known as containers, iterators, and algorithms. As the name suggests, a container represents storage units that hold data elements (data structure). An iterator is like a loop that moves and scans through the container. An iterator that points to the contents of a container can be used to traverse through the elements of that container. Each container has its own algorithms (functions); there are also algorithms that work with all containers. In summary, the algorithms are the programs (functions) that are applied to data structures.