Connecting Tech Pros Worldwide Forums | Help | Site Map

STL templates (and vectors)

D_C D_C is offline
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 294
#1: Jul 9 '06
I'm doing some pre-code planning, I am curious if/how possible the following templates would be? For my example, Class A contains a vector of Class B, which contains a vector of Class C, which contains a vector of Class D. It's quite modular.


Template 1 - trim()
I would like to write a template that can take in a vector, and automatically trim it's capacity down to it's size. I have to do this in two places, with different types of vectors, vector<C> and vector<D>. The code for trim() is really simple:
Expand|Select|Wrap|Line Numbers
  1. vector<object_type>(v).swap(v);
Also, this is a simplified version of Template 2, which I am more interested in using.


Template 2 - cascade()
My plan has a lot of symmetry, it's like class A is the top tier, and then I want the calls to propagate down into the lower tiers. For example in the constructor A() I call constructor B() which calls constructor C() which calls constructor D(). As a result, I have many functions which are pseudocoded:
Expand|Select|Wrap|Line Numbers
  1. // properly declare it as the iterator for vector PARAM1
  2. for(it = PARAM1.begin; it != PARAM1.end; it++)
  3.     it->PARAM2();
I am assuming if we pass a pointer to the vector, it will remember the object type, and size, unlike an array. Is there anything else I should know?

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,188
#2: Jul 11 '06

re: STL templates (and vectors)


Quote:

Originally Posted by D_C

I'm doing some pre-code planning, I am curious if/how possible the following templates would be? For my example, Class A contains a vector of Class B, which contains a vector of Class C, which contains a vector of Class D. It's quite modular.

Not a problem (as long as you stay inside the memory constraints of your system.


Quote:

Originally Posted by D_C

Template 1 - trim()
I would like to write a template that can take in a vector, and automatically trim it's capacity down to it's size. I have to do this in two places, with different types of vectors, vector<C> and vector<D>. The code for trim() is really simple:

Expand|Select|Wrap|Line Numbers
  1. vector<object_type>(v).swap(v);
Also, this is a simplified version of Template 2, which I am more interested in using.

I would be inclined just to create a new emplate class that inherits from vector

Expand|Select|Wrap|Line Numbers
  1. template <class T> class MyVector : public vector<T>
  2. {
  3. public:
  4.     MyVector() : vector() {};
  5.     trim(){vector<object_type>(v).swap(v);};
  6. }
  7.  
Quote:

Originally Posted by D_C

Template 2 - cascade()
I am assuming if we pass a pointer to the vector, it will remember the object type, and size, unlike an array. Is there anything else I should know?

Yes but the type for a vector of pointers is pointer to A and the size (on mist 32 bit systems today) is 4 bytes. You may want a vector of objects in which case you wont be passing pointers.

It's the difference between the 2 declarations

Expand|Select|Wrap|Line Numbers
  1. vector<double> vd;
  2. vector<double *> vpd;
  3.  
Reply