c++ template笔记(2)模板类
生活随笔
收集整理的這篇文章主要介紹了
c++ template笔记(2)模板类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
1.自定義Stack模板類
#include <vector> #include <stdexcept>template <typename T> class Stack {private:std::vector<T> elems; // elementspublic:void push(T const&); // push elementvoid pop(); // pop elementT top() const; // return top elementbool empty() const { // return whether the stack is emptyreturn elems.empty();} };template <typename T> void Stack<T>::push (T const& elem) {elems.push_back(elem); // append copy of passed elem }template<typename T> void Stack<T>::pop () {if (elems.empty()) {throw std::out_of_range("Stack<>::pop(): empty stack");}elems.pop_back(); // remove last element }template <typename T> T Stack<T>::top () const {if (elems.empty()) {throw std::out_of_range("Stack<>::top(): empty stack");}return elems.back(); // return copy of last element }測試
#include <iostream> #include <string> #include <cstdlib> #include "stack1.hpp"int main() {try {Stack<int> intStack; // stack of intsStack<std::string> stringStack; // stack of strings// manipulate int stackintStack.push(7);std::cout << intStack.top() << std::endl;// manipulate string stackstringStack.push("hello");std::cout << stringStack.top() << std::endl; stringStack.pop();stringStack.pop();}catch (std::exception const& ex) {std::cerr << "Exception: " << ex.what() << std::endl;return EXIT_FAILURE; // exit program with ERROR status} }由于多掉了一次pop所以報錯
2.用typdef產生一個特定模板類別名
比如Stack<int>
typedef Stack<int> IntStack; int main() {IntStack intStack;intStack.push(7);std::cout << intStack.top() << std::endl; }3.模板類的特化
#include <deque> #include <string> #include <stdexcept> #include "stack1.hpp"template<> class Stack<std::string> {private:std::deque<std::string> elems; // elementspublic:void push(std::string const&); // push elementvoid pop(); // pop elementstd::string top() const; // return top elementbool empty() const { // return whether the stack is emptyreturn elems.empty();} };void Stack<std::string>::push (std::string const& elem) {elems.push_back(elem); // append copy of passed elem }void Stack<std::string>::pop () {if (elems.empty()) {throw std::out_of_range("Stack<std::string>::pop(): empty stack");}elems.pop_back(); // remove last element }std::string Stack<std::string>::top () const {if (elems.empty()) {throw std::out_of_range("Stack<std::string>::top(): empty stack");}return elems.back(); // return copy of last element }基于模板類之上,對于特定類型進行重新實現,比如上面就是一個對std:: string的特例實現,內部用deque實現
測試
#include <iostream> #include <string> #include <cstdlib> #include "stack2.hpp"int main() {try {Stack<int> intStack; // stack of intsStack<std::string> stringStack; // stack of strings// manipulate int stackintStack.push(7);std::cout << intStack.top() << std::endl;intStack.pop();// manipulate string stackstringStack.push("hello");std::cout << stringStack.top() << std::endl; stringStack.pop();stringStack.pop();}catch (std::exception const& ex) {std::cerr << "Exception: " << ex.what() << std::endl;return EXIT_FAILURE; // exit program with ERROR status} }上面Stack<std::string>使用了特化的類
4.局部特化
若模板類有兩個參數,那么確定其中一個參數,另一個未確定的話,則可以說是局部特化
這些概念與c#的泛型是相同的.
5.預設模板
與局部特化概念差不多,但保留了參數,給參數設置了一個默認的類型
示例:這樣如果用默認的類型,就可以少指定一個模板類的類型
#include <iostream> #include <deque> #include <cstdlib> #include "stack3.hpp"int main() {try {// stack of ints:Stack<int> intStack;// stack of doubles which uses a std::deque<> to manage the elementsStack<double,std::deque<double> > dblStack;// manipulate int stackintStack.push(7);std::cout << intStack.top() << std::endl;intStack.pop();// manipulate double stackdblStack.push(42.42);std::cout << dblStack.top() << std::endl; dblStack.pop();dblStack.pop();}catch (std::exception const& ex) {std::cerr << "Exception: " << ex.what() << std::endl;return EXIT_FAILURE; // exit program with ERROR status} }轉載于:https://www.cnblogs.com/Clingingboy/archive/2011/03/08/1977189.html
總結
以上是生活随笔為你收集整理的c++ template笔记(2)模板类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在线 Sql Server 服务无法启动
- 下一篇: EXTjs grid与json数据 转载