COSC 2436 - Programming Fundamentals III Data Structures
Bob Comer, Professor of Computer Studies


Example Programs
Chapter 9 - Templates and Standard Comtainers

 

Description

Program

A List class template created by:

  • Starting with DList class - dynamic array implementation of a list from the textbook:

    Figure 6.3

  • Following the 3 rules for building a class template from section 9.3 of the textbook.
Since the template implementation must be compiled at the same time as the application, I had to make sure that my IDE did not try to compile the template implementation twice. To do this I took the implementation file "DList.cpp" and renamed it "DList.template". My IDE will not try to separately compile a file with an extension of ".template".

client program file      listdemo.cpp

template header file       DList.h

template implementation file    DList.template

This simple example shows how to create and use a vector. The vector class has a subscript operator that can be used to access elements in the vector. However, the subscript operator cannot be used to add elements to the vector. You must use a member function (like push_back()) to add elements.

vector1.cpp

Some STL containers do not have a subscript operator. All STL containers allow you to use iterators to access the elements in the container. The previous vector example has been rewritten to use iterators.

vector2.cpp

 


Return to Programming Fundamentals III Home Page

Copyright: © 2015 by the Austin Community College
Department of Computer Studies. All rights reserved.
Comments to:
Bob Comer
Last updated: April 5, 2015