Click here to Skip to main content
Click here to Skip to main content

Using STL

, 29 Dec 1999
Rate this:
Please Sign up or sign in to vote.
A brief introduction on using the Standard Template Library


My C++ programming background comes from DOS Borland C++. In their latest DOS version 3.1, they included a template library for collections. It was a great piece of work. When I started to use Visual C++ v2.2, I even tried to use Borland's collection template library within Visual C++ but with no success. The only other solution was to switch to Microsoft's collections that are part of the MFC. However, this was always a problem for the following reasons:

  • It is not easy to switch from one container type to another once the application is written (from list to array for example).
  • Iterators are different for different type of containers.
  • If one is writting a DLL, service or a console application and needs containers, the solution is to either dynamically or statically link MFC which makes the project dependent on MFC.

Recently, I started using STL and it is great. The beginning was a bit difficult but once I grasped the idea, it was easy. This article contains some first hand experiences with STL and is intended for programmers that want to use STL fast and without going into greater details.

Rule 1:

You can create STL containers that store either objects or pointer to objects.

class TMyClass;
typedef list<TMyClass> TMyClassList;     // Stores objects into the list container
typedef list<TMyClass*> TMyClassPtrList; // Stores pointers to object into the list container

Usually, list container that stores the object is used. However, if the object is using a machine resource (handle to file, named pipe, socket or similar), then it is more appropriate to use lists that stores pointers to objects.

If the container stores objects, then it is automatically cleaned up during container destruction. However, if it stores pointers to objects, programmer is responsible to delete all pointers.

Rule 2:

Each class (whose instance will go into the container) must implement at least the copy constructor (it is good to implement also the assignment operator.
class TMyClass {

        // Copy constructor
        TMyClass(const TMyClass& obj)  { *this = obj; }

        // Assignment operator
        TMyClass& operator=(const TMyClass& obj);    

This is necessary since the STL will create a local copy of the object when you insert an object instance into the container. If you do not write a correct code for the copy constructor, object within a list will have some data members uninitialized.

Rule 3:

Inserting an object into the container is done in the following way:

TMyClass object;
TMyClassList myList;
TMyClassList::iterator it;

it = myList.insert(myList.end(), object);
TMyClass *pObject = &(*it);

Previous example shows how to insert an object into the container and obtain a pointer to the object within container. This is necessary since the container will create a new copy of the "object" instance and the original "object" instance is not used any more. In case you are storing pointers to a list, this is not necessary since original pointer is stored into the container.

Rule 4:

Iterating through container is done in the following way:

TMyClassList::iterator it;
TMyClass *pObject;
for (it = myList.begin(); it != myList.end(); it ++) {
    pObject = &(*it);
    // Use pObject 

However, if you are storing pointers into the container, then the previous code fragment has to be modified to the following:

TMyClassList::iterator it;
TMyClass *pObject;
for (it = myList.begin(); it != myList.end(); it ++) {
    pObject = *it;
    // Use pObject 

Rule 5:

Removing items from the container is done in the following way:

TMyClassList::iterator it;
TMyClass *pObject;
for (it = myList.begin(); it != myList.end(); it ++) {
    pObject = &(*it);
    if (pObject satisfies some delete criteria) then
        // If pointers are stored in a list then add
        delete pObject;

Additional line to delete the pointer to the object is needed since the container will not delete a stored pointer so it has to be manually deleted.


The only thing that is missing from STL is a function similar to CString::Format() function.

I hope that this article will give you enough information to start using STL. You will be surprised how easy it is.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Zoran M. Todorovic
Web Developer SCA d.o.o.
Serbia Serbia
I am a cofounder of SCA Software, company that specializes in software for process control, visualization and communication. Programming for the last 10 years in C++, Delphi. Visual C++ for the last 6 years. Degree in Electronics Engineering and Telecommunications.

Comments and Discussions

GeneralMy vote of 4 PinmemberMember 1131211128-Mar-15 5:29 
GeneralMy vote of 3 PinmemberCIDev24-Sep-10 5:13 
GeneralChecking valid pointers PinmemberLordMarv12-Apr-05 5:50 
GeneralVector Pinmemberarvind_tyche10-May-04 4:50 
GeneralRe: Vector Pinmembertoxcct10-May-04 5:28 
GeneralEffective STL PinmemberJonathan de Halleux8-Dec-03 22:35 
GeneralRe: Effective STL Pinmemberpeterchen13-Jul-04 4:26 
GeneralRule 2: Copy constructor not required PinmemberAndrew Phillips8-Dec-03 22:13 
GeneralMany many ambiguous symbol errors PinmemberJWood21-Aug-03 6:11 
GeneralError: No copy contructor available Pinmemberminty11-Feb-03 16:15 
GeneralRe: Error: No copy contructor available PinmemberChristian Graus11-Feb-03 17:01 
GeneralRe: Error: No copy contructor available Pinmemberminty12-Feb-03 11:57 
GeneralCString::Format() PinsussMoak19-Aug-02 22:27 
GeneralRe: CString::Format() PinsussAnonymous7-Aug-03 0:54 
QuestionAre string templates threadsafe? PinmemberNidhi Narang7-Dec-01 8:05 
AnswerRe: Are string templates threadsafe? PinmemberBryan Labutta7-Feb-02 9:39 
GeneralRe: Are string templates threadsafe? PinmemberMatthew Rees-George26-Mar-02 17:03 
GeneralRule 5 removing items PinsussMichael S. Scherotter18-Mar-00 5:39 
GeneralThanks! Pinmemberclintsinger21-Nov-01 20:52 
GeneralRe: Thanks! PinmemberMichael S. Scherotter25-Nov-01 13:16 
GeneralRe: Rule 5 removing items PinsussMoak19-Aug-02 22:34 
GeneralMissing Format - use std::ostringstream PinsussKen Nicolson31-Jan-00 14:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150331.1 | Last Updated 30 Dec 1999
Article Copyright 1999 by Zoran M. Todorovic
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid