我想使用以下結構C++,模板化T IN的std ::對<T, short>
template <typename T>
struct TPair
{
typedef std::pair <T, short> Type;
};
到模板化「第一」的std ::對類型和創建這樣對一個矢量。
template <typename T>
struct TPairs
{
typedef std::vector <TPair <T> > Type;
};
但這個代碼似乎被擰爲了共同使用,這是不舒服:
TPair <double> ::Type my_pair (1.0, 0); //Create pairs
TPair <double> my_pair2 (1.0, 0); //Create object, needs a constructor
TPairs <double> ::Type pairs; //Create vector
TPairs <double> pairs2; //Create object
pairs.push_back(my_pair); //Need a constructor
pairs2.push_back(my_pair); //No push_back method for the structure...
....
有沒有更簡單舒適的解決方案?
此外,你不應該''typedef''定義'對'的'vector',你從而沒有聲明一個向量,而是一個別名類型。 –
@泰勒先生:我認爲這是故意的。我認爲OP需要一個「typedef模板」。我認爲這些被添加到C++ 11的標準中。 –