ComputerSimulation.org
 C++ Pointers C++ PointersObjects, structs and STL Containers
Introduction
Pointer #1Objects & STL containers
Pointer #2When a member function and variable name collide
Nearly all the literature (both on-line and textbooks) we referenced to learn how to use STL containers only showed how to use them to hold primatives, (e.g. integers and strings), but made no mention of how to handle objects or structs stored in a container.

This "C++ Pointer" shows you how to manipulate objects and structs stored in an STL Vector and an STL Map. There are other STL containers, but once the use of these two containers is known, it should be possible for you to work out how to use the others.

As far as referencing objects, the difference between the Vector and the Map containers is that Vectors store only the objects, whilst the Map stores a key to reference the object, as well as the object.
 
Overview of the Example Programmes
To be easily understandable for everyone, the example code is based on a simple Class (ColourCode.cpp) and struct (ColourCodeStruct) that contains the name of a colour and its Red, Green and Blue colour values. How to use this object and struct in an STL Vector is shown in VectorExample.cpp and in MapExample.cpp for an STL Map.

The example programmes are identical in design. A container to hold the ColourCode objects/structs is created and then passed by reference to an object of Loader. This loads the container with ColourCode objects/structs which contain the colour information for the basic web browser colours.

Then an Iterator for the container is created and is used to step through the contents of the container. For each object/struct in the container details of the object/struct are extracted and written to a file, (VectorExample_res.txt or MapExample_res.txt). Then the colour named "custom1" is located in the container and its properties modified to khaki, The contents of the container are then re-written to the file.

Notes
1.the parameters for ColourCode.getName(Name) and ColourCode.getRGB(red,green,blue) methods are passed by reference. This is done for performance reasons and to reduce the chance of memory leaks, (through the creation of objects that must then be managed). The code for .getName(name) (at least) could be changed to,

name = cCOItr->getName();

if you want to. Of course the signiture of ColourCode.getName() must be changed too.

This "C++ Pointer" assumes you know how to use a Vector or Map to hold primatives, as this is widely described in literature. If you want to know how to use the STL Vector or Map check out www.cppreference.com/cppstl.html for information on the Standard Template Library.

Please note that this "C++ Pointer" is distributed with NO WARRANTY.
 
How to Create a Map and Vector of objects or structs
A Vector or Map of objects is created in the same way that a Vector or Map of primatives is created. An example of the simplest manner in which they are created is as follows;

vector<ColourCode> colourCodeObjects;
map<string,ColourCode> colourCodeObjects;

The creation of a Vector or Map of structs is a bit more convoluted. An example of the simplest manner in which they are created is as follows;

vector<ColourCodeStruct> colourCodeStructs = vector <ColourCodeStruct>();
map<string,ColourCodeStruct> colourCodeStructs = map <string,ColourCodeStruct>();
 
How to Create an Iterator for STL Maps and Vectors of objects or structs
An Iterator for Vector or Map of objects or structs is created the same way as if it was being created for a Vector or Map of primatives.
 
How to Access the Method of an Object Stored in an STL Vector
To access the methods of an object in a Vector you create an Iterator to the Vector, (cCOItr in this example) and then treat the iterator as a pointer to the object and use the -> (structure pointer dereference) operator in place of the dot operator to access the object's methods. For example;

cCOItr->getName(name); acts like colourCode.getName(name);
 
How to Access the elements of a struct Stored in an STL Vector
To access the elements of a struct in a Vector you create an Iterator to the Vector, (cCSItr in this example) and then treat the iterator as a pointer to the struct and use the -> (structure pointer dereference) operator as normal. For example;

cout << cCSItr->name;
 
How to Access the Method of an Object Stored in an STL Map
To access the methods of an object in a Map you create an Iterator to the Map, (cCOItr in this example) and use it to select the object you are interested in as applicable. Use the -> (structure pointer dereference) operator to access the object which is the second parameter of the iterator and then use dot notation to access the object's method as follows;

cCOItr->second.getName(name); acts like colourCode.getName(name);
 
How to Access the elements of a struct Stored in an STL Map
To access the elements of a struct in a Map you create an Iterator to the Map, (cCSItr in this example) and use it to select the struct you are interested in as applicable. Use the -> (structure pointer dereference) operator to access the struct which is the second parameter of the iterator and then use dot notation to access the element of the struct. For example;

cout << cCSItr->second.name; acts like cout << colourCodeStruct->name;
 
Software for Download and Installation
The example programmes for this "C++ Pointer" were compiled and tested using gcc version 4.1.1 running under Fedora Core 6. They were also compiled and tested on a WinXP box using cygwin-1.5.20-1, gcc version 3.4.4.

To obtain the example programmes for this "C++ Pointer" use the following procedure.
1.Download and save to disk CSOCppPtr1.tar.gz  (9.5kB)
2.Start a console and navigate to the directory you saved the zipped tar file in. Extract the source with tar zxvf CSOCppPtr1.tar.gz This will create a directory CSOCppPtr1 and place the source files in it.

Once the programme files have been extracted use your favourite editor to view them and/or compile and run using the following commands;
$ make
$ ./VectorExample
$ ./MapExample
 
Revision History
Rev Date Description
1.0 05-Nov-2005 Initial Version Released
2.0 30-Dec-2006 Modified to include how to use structs
 
Technical Support
Frequently Asked Questions
Currently there are no FAQ for this "C++ Pointer".
If you have a question, please e-mail it to us. Please ensure that you identify that your question is about our C++ Pointer on Objects, structs and STL Containers.

Bug-Reports
Currently there are no reported bugs for this "C++ Pointer".
If you suspect a bug in this "C++ Pointer", before contacting us, please check the FAQs and Bug Reports to see if it has already been identified.
If it hasn't, e-mail a description to us. Please ensure you identify that the bug report is about our C++ Pointer on Objects, structs and STL Containers and include as much detail as possible.
Last modified 17 May 09