|
From: Anders N. <An...@ek...> - 2002-03-25 15:10:12
|
A very good and pedagogical explonation. Answers like this make this =
list great!
Anders Nilsson
----- Ursprungligt meddelande -----=20
Fr=E5n: "Graeme Foster" <gr...@dr...>
Till: <dev...@li...>
Skickat: den 25 mars 2002 15:24
=C4mne: [Dev-C++] vectors
> What is a vector?
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> In short a vector one of the containers provided by the Standard =
Template
> Library. A container is a data type that allows objects of a similar =
type to
> be held together. A template allows the data type to be declared when =
the
> container is created. The characteristics of the vector is that it is
> dynamic - that means that when a new element is to be added to the =
container
> then that is done automatically. New elements are added to the end of =
the
> vector, for most efficient results the elements should also be deleted =
from
> the end but it is possible to delete elements from anywhere from =
within the
> vector. Elements in the vector can be accessed randomly using the []
> operators just like the array.
>=20
> So is a vector like an array?
> =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
> I guess that you could say that it's like an array on steroids. It can =
do
> much more than a simple array but with a familiar interface.
>=20
> When should an array be used and when should a vector be used?
> =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> If you know before hand how many elements will be used then use an =
array, if
> however the number of elements is not known then use a vector.
>=20
> What did you mean by a vector allowing "objects of a similar type to =
be held
> together"?
> =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> The similar type refers to classes within the class hierarchy. The =
class
> defined as the template can be used along with any derived class. =
Hence a
> vector of person objects will also be able to hold students objects =
and
> lecturer objects, so long as both of these objects are derived from =
the
> person class.
>=20
>=20
> Please give me an example!
> =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D
>=20
> The following may help to clarify things:
>=20
> I have declared a class called allObjects which contains a vector of
> instances of the cObject class:
> +++++++++++++++++++++++++++++++++++
> class cAllObjects
> {
> private:
> vector<cObject *> list;
> public:
> void add(cObject * obj);
> void remove (cObject * obj);
> cObject * get (long OID);
> void dump();
> };
>=20
> The definitions are as follows:
> +++++++++++++++++++++++++++++++++++
> void cAllObjects::add(cObject * obj)
> {
> list.push_back(obj);
> }
>=20
> void cAllObjects::remove (cObject * obj)
> {
> list.erase(find(list.begin(), list.end(), obj));
> }
>=20
> cObject * cAllObjects::get (long OID)
> {
> unsigned int i =3D 0;
> while (i < list.size() && list[i]->OID !=3D OID)
> {
> i++;
> }
> if (i >=3D list.size())
> return 0;
> else
> return list[i];
> }
>=20
> void cAllObjects::dump()
> {
> for (int i =3D 0; i < list.size(); i++)
> list[i]->dump();
> }
>=20
> For this to work the cObject needs to be declared and defined:
> +++++++++++++++++++++++++++++++++++
> class cAllObjects;
> class cObject
> {
> friend cAllObjects;
> private:
> static long nextOID;
> static cAllObjects allObjects;
> long OID;
> string className;
> public:
> static void dumpAll();
> cObject(long pOID =3D nextOID);
> ~cObject();
> void dump();
> };
>=20
> and:
> +++++++++++++++++++++++++++++++++++
> #include "Object.hpp"
>=20
> // Initialise the Static members
> long cObject::nextOID =3D 0;
> cAllObjects cObject::allObjects;
>=20
> void cObject::dumpAll()
> {
> allObjects.dump();
> }
>=20
> // Constructors
> cObject::cObject(long pOID)
> {
> // Add this object to the list
> allObjects.add(this);
> // Check that the OID hasn't yet been used
> if (pOID < nextOID)
> pOID =3D nextOID;
> OID =3D pOID; // assign the unique number to OID
> nextOID =3D pOID + 1; // increment the nextOID by 1
> className =3D "Object";// set the class name to Object
> }
>=20
> // Destructors
> cObject::~cObject()
> {
> // Remove this object from the AllObjects list
> allObjects.remove(this);
> }
>=20
> // Diagnostic methods
> void cObject::dump()
> {
> cout << "OID =3D " << OID << "\n";
> cout << "Class =3D " << className << "\n";
> }
>=20
> This can then be tested using the following main:
> int main(int argc, char* argv[])
> {
> cObject myObject1(5);
> myObject1.dump();
> cObject myObject2(0);
> myObject2.dump();
> cObject myObject3(0);
> myObject3.dump();
> cObject myObject4;
> myObject4.dump();
> cObject::dumpAll();
> cout << "Press any key to finish...\n";
> getch();
> return 0;
> }
>=20
> Files
> =3D=3D=3D=3D=3D
> The declarations of both classes are held in the same file called
> Object.hpp, whilst the source code was held in the their own files.
>=20
> This enables an object to hold all of teh objects that have been =
created,
> they are put onto the vector from within the constructor of the =
cObject
> class. They can all be listed by using the static member function =
dumpAll()
>=20
>=20
>=20
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>=20
|