|
-
February 23rd, 2003, 05:06 PM
#1
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.
-
February 23rd, 2003, 05:39 PM
#2
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
-
February 24th, 2003, 11:15 AM
#3
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
-
February 24th, 2003, 11:34 AM
#4
...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
-
February 24th, 2003, 11:39 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|