CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2002
    Location
    CA
    Posts
    37

    Templates and Vectors

    I was just curiuos. I'm making an implementation of a heap and I am considering using vectors. But I need to use templates as well. I don't know why it wouldn't work, but can I use templates in vectors?
    example:

    template<class T>
    vector<T> myvector;

    would that work?

    thanks.

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    vector's are already templatized so I suspect your syntax is a
    little off. If you're making your own templatized class, it can,
    indeed, contain a vector data member for the type.
    Consider:
    Code:
    #include <vector>
    using namespace std;
    
    template <typename T>
    class MyClass
    {
    private:
       vector<T> m_vector;
    };
    
    
    void main()
    {
       MyClass<int> intObject;
    }
    --Paul

  3. #3
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Paul, I think he may want to do this (just a guess of coarse):

    Code:
    template <class T>
    class MyTemplate
    {
        // ...
    };
    
    typedef std::vector<MyTemplate<SomeClass>> MyVector;
    And yes, this is possible.

    - Kevin

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    ...with a space between the '>' symbols, of course.

    Sorry, I'm in a pedantic mood today
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  5. #5
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245
    Yes, of coarse -- I think I fall into that trap about every time I write a template that contains a template!!! You think I would have learned by now!

    - Kevin

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured