STL templates (and vectors)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • D_C
    Contributor
    • Jun 2006
    • 293

    STL templates (and vectors)

    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:
    Code:
    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:
    Code:
    // properly declare it as the iterator for vector PARAM1
    for(it = PARAM1.begin; it != PARAM1.end; it++)
        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
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.


    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:
    Code:
    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

    Code:
    template <class T> class MyVector : public vector<T>
    {
    public:
        MyVector() : vector() {};
        trim(){vector<object_type>(v).swap(v);};
    }
    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

    Code:
    vector<double> vd;
    vector<double *> vpd;

    Comment

    Working...