Click to See Complete Forum and Search --> : Templates and Vectors


Evrardo
February 23rd, 2003, 04:06 PM
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.

PaulWendt
February 23rd, 2003, 04:39 PM
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:

#include <vector>
using namespace std;

template <typename T>
class MyClass
{
private:
vector<T> m_vector;
};


void main()
{
MyClass<int> intObject;
}


--Paul

KevinHall
February 24th, 2003, 10:15 AM
Paul, I think he may want to do this (just a guess of coarse):


template <class T>
class MyTemplate
{
// ...
};

typedef std::vector<MyTemplate<SomeClass>> MyVector;


And yes, this is possible.

- Kevin

Graham
February 24th, 2003, 10:34 AM
...with a space between the '>' symbols, of course.

Sorry, I'm in a pedantic mood today :)

KevinHall
February 24th, 2003, 10:39 AM
Yes, of coarse -- I think I fall into that trap about every time I write a template that contains a template!!! :D You think I would have learned by now!

- Kevin