Re: [問題] template stack

看板C_and_CPP (C/C++)作者 (好人超)時間16年前 (2010/03/18 16:34), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串3/4 (看更多)
先感謝 littleshan 的回覆 我修改之後,還有一點小問題: 我必須要明確寫出 allocator<T>,否則無法編譯 有沒有辦法讓我不要寫,而是讓 Compiler 自行推導出來呢? #include <iostream> #include <vector> #include <list> using namespace std; template <typename T = int, template<typename, typename> class Cont = vector> class Stack { public: void push(T val) { cont.push_back(val); } void pop() { cont.pop_back(); } T top() /* 有更好的寫法嗎?總覺得這麼寫不太妥... */ { Cont<T, allocator<T> >::iterator iter = cont.end(); return *(--iter); } private: Cont<T, allocator<T> > cont; }; int main() { Stack<double, list> s; for (int i = 0; i < 5; i++) s.push(i * 0.5); for (int i = 0; i < 5; i++) { cout << s.top() << endl; s.pop(); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.117.166.145

03/18 17:00, , 1F
再加上 template<typename> class V = allocator?
03/18 17:00, 1F
文章代碼(AID): #1BeUMMdM (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1BeUMMdM (C_and_CPP)