Skip to main content

Standard Template Library

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.
stl.jpg
In this Module we’ll discuss and learn about containers( Vectors and List).

Resources

Web Resources:
STL:
PPT:


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:
      1. STL description
      2. STL vector
      3. STL list
  • 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.
Spend atleast 2-3 Hours from step 1 - step 3.


Problem Sets


Problem Set A

1. Create a vector containing the letters of the alphabets in order. Print the elements of that vector in order and in reverse order.
Name the program as: PA1_vector.cpp

Problem Set B

1. Create a list and read a list of names of fruits from cin into it. Sort the list and print it.
Name 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

Popular posts from this blog

Mini Project - Railway Reservation System

Module 1 Overview Railway Reservation System-(RRS) Project is developed to help students learn and realize capability of C programming language, and appreciate procedural approach of developing a system. RRS helps users reserve/cancel berths, search for trains, check reservation/cancellation history, etc... On the other hand it helps the railway reservation officers to prepare reservation charts, organize train schedules, etc... RRS provides two types of log-ins one is the User log-in for railway customers and the other one is Admin log-in for railway reservation officers. Project is divided into 10 modules of four hours duration each. All the modules will give exactly what to develop in the 4 hour time frame. Since most of the modules are inter-related it is important to keep track of the previous code. After submission it is allowed to make changes in the previous code to suit / integrate well to the system. Before we start the project, we need to make the followin...

Inheritance

Week 1 - M5 : Inheritance Learning Objectives At the end of this module, you will be able to Understand the implementation of inheritance in C++ Override the base class members Access overridden base class members using the scope resolution operator Understand the concept of base class initialization Introduction Inheritance The philosophy behind inheritance is to portray things as they exist in the real world. As inheritance is found in real world, it is important feature of OO programming. Inheritance has many advantages, the most important of them being the resuability of code. Once a class is defined and debugged, it can be used to create new subclasses. The reuse of existing class saves time and effort. The Class from which another class is derived is called the base class . The class which inherits the properties of the base class is called the derived class . Each instance of the derived class includes all the members of the base class. Since...