|
From: Alan T. <ala...@3n...> - 2001-01-14 13:14:02
|
Chad,
Thanks for the suggestions. Vectors are exactly what I need.
They were designed for this sort of application. Alan
From: "Chad Simmons" <ho...@ho...>
Subject: Re: [Dev-C++] RE: Dynamic Arrays
Date: Fri, 12 Jan 2001 17:46:35 -0800
From what I saw of what you plan to do I would recommend using STL vectors.
They are almost exactly like arrays except they can grow as needed to hold
as much as your ram will allow. To find out more about vectors (And the rest
of the standard C++ template library) check out these websites..
|
|
From: Graeme F. <gr...@dr...> - 2002-03-25 14:26:39
|
What is a vector?
=================
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.
So is a vector like an array?
=============================
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.
When should an array be used and when should a vector be used?
==============================================================
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.
What did you mean by a vector allowing "objects of a similar type to be held
together"?
============================================================================
===========
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.
Please give me an example!
==========================
The following may help to clarify things:
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();
};
The definitions are as follows:
+++++++++++++++++++++++++++++++++++
void cAllObjects::add(cObject * obj)
{
list.push_back(obj);
}
void cAllObjects::remove (cObject * obj)
{
list.erase(find(list.begin(), list.end(), obj));
}
cObject * cAllObjects::get (long OID)
{
unsigned int i = 0;
while (i < list.size() && list[i]->OID != OID)
{
i++;
}
if (i >= list.size())
return 0;
else
return list[i];
}
void cAllObjects::dump()
{
for (int i = 0; i < list.size(); i++)
list[i]->dump();
}
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 = nextOID);
~cObject();
void dump();
};
and:
+++++++++++++++++++++++++++++++++++
#include "Object.hpp"
// Initialise the Static members
long cObject::nextOID = 0;
cAllObjects cObject::allObjects;
void cObject::dumpAll()
{
allObjects.dump();
}
// 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 = nextOID;
OID = pOID; // assign the unique number to OID
nextOID = pOID + 1; // increment the nextOID by 1
className = "Object";// set the class name to Object
}
// Destructors
cObject::~cObject()
{
// Remove this object from the AllObjects list
allObjects.remove(this);
}
// Diagnostic methods
void cObject::dump()
{
cout << "OID = " << OID << "\n";
cout << "Class = " << className << "\n";
}
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;
}
Files
=====
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.
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()
|
|
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
|
|
From: Ioannis V. <no...@ho...> - 2002-03-26 19:40:52
|
> -----Original Message----- > From: dev...@li... > [mailto:dev...@li...] On Behalf > Of Graeme Foster > Sent: Monday, March 25, 2002 4:24 PM > To: dev...@li... > Subject: [Dev-C++] vectors > > When should an array be used and when should a vector be > used? ============================================================== > 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. No, always use vectors and if in need to squeeze every cycle out in terms of speed use valarrays (they have the speed of optimised Fortran arrays). Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.cjb.net * Alternative URL: http://run.to/noicys |
|
From: A. Jans-B. <jan...@wx...> - 2001-01-17 21:33:26
|
I want to create a 3D modeller in C++ and I am looking for existing material to setup 3D classes (objects like spheres, planes, torusses, etc.) and 3D scenes (with camera's, lights, etc). Any "links" available? |