468,456 Members | 1,563 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,456 developers. It's quick & easy.

STL templates (and vectors)

293 100+
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
1 2604
Banfa
9,051 Expert Mod 8TB
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.

Similar topics

22 posts views Thread by E. Robert Tisdale | last post: by
2 posts views Thread by kelvSYC | last post: by
7 posts views Thread by Jon Slaughter | last post: by
4 posts views Thread by John Goche | last post: by
3 posts views Thread by esuvs81 | last post: by
reply views Thread by JonLT | last post: by
1 post views Thread by Rob | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.