By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

STL templates (and vectors)

100+
P: 293
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?
Jul 8 '06 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
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.


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.  
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.  
Jul 10 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.