2013-03-03 54 views
-1

载体有:发送类型为C++类

vector<type> name; 

如何创建这个“<type>”为我自己的类来通过类型?

+0

你到底在问什么?如何使用'vector'?或者如何编写自己的版本? – 2013-03-03 00:22:00

+0

或者你问自己的班级如何使用模板? – 2013-03-03 00:24:14

回答

0

创建一个类template

例如

template< class item_t > 
class my_vector_t 
{ 
    // whatever 
}; 
2

你在找什么是template

#include <iostream> 
template<typename T> 
class myClass 
{ 
public:  
    myClass(T value){std::cout << value;} 
}; 

int main() 
{ 
    myClass<int> c(1); 
} 
+0

这就是它的感谢 – 2013-03-03 00:29:34

+0

@RafałWądołowski请注意你传入的是什么类型。在这里,构造函数可以工作,因为定义了“cout <<(int)”。如果您要传入'AnotherClass',则必须确保您重载AnotherClass的'<<'运算符(或其他您希望使用的运算符)。 – Ephemera 2013-03-03 00:49:51

+0

@PLPiper确实如此,这个例子对于模板来说肯定不是一个完美的例子。但它只是一个简短的例子。这就是为什么我链接到他想要更深入的维基百科页面。 – Caesar 2013-03-03 00:57:46

0

我以为你是问使用载体的情况要放什么东西来代替type。这只是你的班级的名字:

vector<MyClass> myVec;