Learning Objective
At the end of this module, you will be able to:- Understand Standard Template Library
- Explain the need of STL
- Understand and use the vector and list of STL.
- Using real world objects with STL.
Standard Template Library
Introduction
The Standard Template Library (STL) is a fundamental part of the C++ Standard, is a C++ library of container classes, algorithms, and iterators; it provides many of the basic algorithms and data structures of computer science. The STL is a generic library, meaning that its components are heavily parameterized: almost every component in the STL is a template. You should make sure that you understand how templates work in C++ before you use the STL.The STL contains several kinds of entities. The three most important are containers, algorithms,and iterators.
A container is a way that stored data is organized in memory. In earlier chapters we’ve explored two kinds of containers: stacks and linked lists. Another container, the array, is so common that it’s built into C++ (and most other computer languages). However, there are many other kinds of containers, and the STL includes the most useful. The STL containers are implemented by template classes, so they can be easily customized to hold different kinds of data.
Algorithms in the STL are procedures that are applied to containers to process their data in various ways. For example, there are algorithms to sort, copy, search, and merge data. Algorithms are represented by template functions. These functions are not member functions of the container classes. Rather, they are standalone functions. Indeed, one of the striking characteristics of the STL is that its algorithms are so general. You can use them not only on STL containers, but on ordinary C++ arrays and on containers you create yourself. (Containers also include member functions for more specific tasks.)
Iterators are a generalization of the concept of pointers: they point to elements in a container. You can increment an iterator, as you can a pointer, so it points in turn to each element in a container. Iterators are a key part of the STL because they connect algorithms with containers.
Think of them as a software version of cables (like the cables that connect stereo components together or a computer to its peripherals).
Figure below shows these three main components of the STL.
In this Module we’ll discuss and learn about containers( Vectors and List).
Resources
Web Resources:STL:
PPT:
- Chapter 28: Standard Template Library--> How to Program in C++ by Dietle and Dietle
Step to attack the problem sets
- Step 1: Go through the introduction part and have an understanding about STL.
- Step 2: Go through the Web Resource 1 (Try the problem sets given)
- C++ STL Tutorial Contents:
- STL description
- STL vector
- STL list
- C++ STL Tutorial Contents:
- Step 3: Go through the PPT given (Try the problem sets given in that)
- Step 4: Attack the problem sets in the given order.
- Step 5: If you are not clear with the problem sets then contact your respective mentor for clarification.
Problem Sets
Problem Set A
1. Create a vectorName the program as: PA1_vector.cpp
Problem Set B
1. Create a listName the program as: PB1_list_sort.cpp
Problem Set C
1. Write two separate functions for Week 2 Module 1 Problem Set A - 1(cricket Stats).Function 1: Load all the data from the binary file into the List and return the List.
Function 2: Load all the data from the binary file into the Vector and return the Vector.
Comparison of two instances of Player should be based on Name.
Override the operators = , == and <>
Print all the player details sorted on Player Name Using List.
Print all the player details using Vector.
Comments
Post a Comment