Broad Network

Introduction to a Simple C++ Sequence Container

Container Library Sequences in C++ Simplified - Part 1

Forward: In this part of the series, I introduce a very simple container.

By: Chrysanthus Date Published: 24 Aug 2012


This is the first part of my series, Containers Library Sequences in C++, Simplified. A container is an instantiated object, which has a list (array) as its fundamental feature. Unlike an array list, a container can grow or shrink in size (length). Different containers have different properties and methods. In this part of the series, I introduce a very simple container.

There are two types of container classes, called Sequences and Associative Containers. This series deals with Sequences. I will write a different series for Associative Containers. This series is broken down into divisions. A division has at least one part. The first three parts are in division 1. Division 1 is an introduction to Sequences.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at That is, contact me for the slightest problem you have about what you are reading.

C++ Standard Library
C++ installation comes with a library that has important and common features (ready-made code segments) that you can use, without writing code for the features. This library is called the standard library. The library is divided into categories. These categories are still called libraries. The categories are divided into components. Components are divided into entities. Entities are basic things like data types and functions.

There are many header files for the standard library. You access a feature (entity) through a header file. With a typical C++ installation, you do not need to know where (directory) the standard library or standard libraries are kept; you do not need to even know where the header files for the standard library are kept. With my installation (mingw), all you need to know is the name of the header files. To use the header files, you just need to include it at the top of your code (without indicating the path). You do that using the #include preprocessing directive with the name of the header file alone in angle brackets.

How do you know which feature is in a header file. One way to know that is by reading articles, which are of reference nature like this one (series).

What I give you in this series is a simplified version of what is in the ISO/IEC 14882:2003 C++ specification.

There are other articles (tutorials) I have written in this blog on C++. You need to have read them or articles similar to them before reading this series. The titles of the articles in this blog, which are prerequisite to reading this series are:

- Getting Started with C++
- OOP Basics in C++
- C++ Function Templates
- Dynamic Objects in C++
- Integer and Float Object Types in C++
- Reference in C++
- Assignment Operators in C++
- Basics of Exceptions in C++
- User Types from Fundamental Types in C++
- Pointer to Function in C++
- C++ String Class Basics
- An in-depth Look into C++ Core String
- Exception Classes in C++
- Introduction to C++ Standard Language Support Library
- The Reserved Word const in C++

A computer language builds up. There are certain things you have to learn first and then use them to learn higher things. Each of the above titles is either a tutorial or the first tutorial in a series. If it is the first part of a series, then you should have read the whole series. If it is a tutorial standing alone, then you should have read the tutorial. To reach any of the articles, just type the title of the article and my name Chrys in the Search Box of this page and click Search.

Templates and Containers
An ordinary list (array) is created to take only one type of objects for all its elements. For example, all its elements might be ints, all its elements might be floats, all its elements might be pointers, etc. A container behaves like this but has additional features for its list.

C++ has only one type of ordinary list, which is the array. A container is instantiated from a class. The array is not instantiated from a class. We can make a container class, a template. In that way, in the same scope and same program, we can instantiate a container object now from a class to hold ints; the next minute and in the same scope and same program, we can instantiate another container from the same class to hold floats; the next minute, in the same scope and same program, we can instantiate another container to hold pointers. Can you see an advantage? we use the same class to have arrays (objects) that will hold different object types in the same scope. You cannot have the same array to hold different object types in the same scope. Because a container object is instantiated from a class, it is like we have the same array holding different objects types at the same time. Since a container is instantiated from a class, another advantage follows: a container has properties and methods; the array does not have any property or method. For example, a container has a property that can make it grow in size at run time. In C++ an array is not an instantiated object and it cannot grow.

Many features (components) in the standard library use templates. The string Library (you should have read the article, String in C++ Standard Library Simplified) uses class templates. All containers use templates.

Dynamic Memory
Many standard library components use dynamic memory (free store). The string library uses dynamic memory. The containers library, whose sequence containers, we learn in this series, uses dynamic memory.

A Simple C++ Container
For the first three parts of the series, we shall come up with a simple C++ container. In the derivation of the simple container, we shall take things step by step. There are two broad steps as follows: We start with an ordinary array and then look at its disadvantages. Then we treat the simple container. These major steps will have smaller steps.

Ordinary Array
The following code shows two ordinary arrays in the scope (block) of the maim function.

#include <iostream>
using namespace std;

int main()
        int intArr[] = {6, 6, 6, 6, 6};
        char charArr[] = {'H', 'H', 'H', 'H', 'H'};

        return 0;

The first array has five ints, each of value, 6. The second array has five chars each of value, H.

These arrays have the disadvantage that the size cannot be increased or decreased (officially). An element of an array has both value and the object that holds the value. These arrays have another disadvantage that you cannot erase any of its objects to end up with a lesser number of elements.

When we want a set of ints we create an array for that. When we want a set of chars, we create an array for that. Is it possible to have one kind of list structure that will hold a set of ints at one time and hold a set of chars (or something else) at another time, in the same scope? Yes, it is possible today, but not with the array. There are other disadvantages. Today, these disadvantages are called limitations of the C++ array. These limitations gave rise to development of containers.

In order for a list to grow to a very large size and at run time, you have to use dynamic memory. A container is a template class with a list; this list is in dynamic memory. The template feature in a container is the fact that the list can have different object types (int now, next time chars, etc).

The advantage the class feature brings in a container is that the class has properties and methods. These properties and methods are used to overcome many of the limitations of the C++ array.

A Simple Container Constructor
A container is a class template list. Such a class has properties and methods, which makes the list a better form of the C++ array (overcoming the limits of the array). In order for the list to grow in length at run time, the list should reside in dynamic memory.

The following program shows a simple container with one method, which is the constructor method.

#include <iostream>
using namespace std;

template <class T>
class AdvancedList
        AdvancedList(int noIniCells, T defaultObj)
                T *listPtr = new T[noIniCells];
                for (int i = 0; i < noIniCells; ++i)
                        listPtr[i] = defaultObj;

int main()
        AdvancedList<int> myObject(5, 6);

        return 0;

There are two segments in the code: the class template and the main function. The first line in the first segment declares the template. The class description should follow this declaration. In the first line, T is a placeholder. If you want your list to be all ints, T would represent int; if you want your list to be all chars, T would represent chars, and so on.

The first argument in the constructor is the initial number of elements that the list would have. The second argument is the default (initial) value for all the elements. Note: The values in your list do not only have to be of a fundamental object type. They can be instantiated objects of a class.

The first line in the constructor block creates the list with the number of elements in dynamic memory, but with no values. The for-loop construct that follows, puts the default values in the elements of the list. Having default values for the elements is not compulsory; you can still write code that will not put the default values.

In the main function the class template is instantiated. In the instantiation, what you put in the angle brackets is the object type you want for the values in the list. The content of any such pair of angle brackets is called template argument (s). For the constructor arguments here, 5 is the initial number of elements for the list, while 6 is the default value for all the elements in the list.

Well, we have seen enough in this part of the series. Let us take a break and continue in the next part.


Related Links

C++ Taking the Bull by the Horns
C++ Just After the Basics
Object Oriented Programming in C++
C++ Operators
C++ Templates
Exception Handling in C++
Container Library Sequences in C++ Simplified
Associative Container in C++ Simplified
String in C++ Standard Library Simplified
Date and Time in C++ Simplified
Advanced Course
Storage Duration in C++
Scopes in C++
Function and Operator Overloading in C++
Specifiers in C++
Some Features of C++ Entities
C++ Preprocessing Directives
Writing a C++ Container
C++ Namespace
Writing a C++ Application
More Related Links
Relational Database and Sybase
Windows User Interface
Computer Programmer A Jack of all Trade Poem
C++ Course


Become the Writer's Fan
Send the Writer a Message