c++模板使用

Templates -c++ Tutorials
In C++ this can be achieved using template parameters. A template parameter is a special kind of parameter that can be used to pass a type as argument: just like regular function parameters can be used to pass values to a function, template parameters allow to pass also types to a function. These function templates can use these parameters as if they were any other regular type.
template

队列
  • 队列特征:
    先进先出(只可以操作头部)
  • 队列时间复杂度:
    查找:O(n)
    插入:O(1) 即入队
    删除:O(1) 即出队

源码分析:

  1. template <class Elem>
  2. class Queue
  3. {
  4. public:
  5. Queue(int MaxSize=500);
  6. Queue(const Queue<Elem> &OtherQueue);
  7. ~Queue(void);
  8. void Enqueue(const Elem &Item); // Adds Item to Queue end
  9. Elem Dequeue(void); // Returns Item from Queue
  10. inline int ElemNum(void); // Returns Number of Elements
  11. protected:
  12. Elem *Data; // The actual Data array
  13. const int MAX_NUM; // The actual spaces will be one more than this
  14. int Beginning, // Numbered location of the start and end
  15. End;
  16. // Instead of calculating the number of elements, using this variable
  17. // is much more convenient.
  18. int ElemCount;
  19. };

%23%23%23%23%23%20c%2B%2B%E6%A8%A1%E6%9D%BF%E4%BD%BF%E7%94%A8%0A%5BTemplates%20-c%2B%2B%20Tutorials%5D(http%3A%2F%2Fwww.cplusplus.com%2Fdoc%2Foldtutorial%2Ftemplates%2F)%0AIn%20C%2B%2B%20this%20can%20be%20achieved%20using%20template%20parameters.%20A%20template%20parameter%20is%20a%20special%20kind%20of%20parameter%20that%20can%20be%20used%20to%20pass%20a%20type%20as%20argument%3A%20just%20like%20regular%20function%20parameters%20can%20be%20used%20to%20pass%20values%20to%20a%20function%2C%20template%20parameters%20allow%20to%20pass%20also%20types%20to%20a%20function.%20These%20function%20templates%20can%20use%20these%20parameters%20as%20if%20they%20were%20any%20other%20regular%20type.%0Atemplate%20%3Cclass%20myType%3E%0A%0A%0A%23%23%23%23%23%20%E9%98%9F%E5%88%97%0A%0A%20%E9%98%9F%E5%88%97%E7%89%B9%E5%BE%81%EF%BC%9A%0A%20%20%E5%85%88%E8%BF%9B%E5%85%88%E5%87%BA%EF%BC%88%E5%8F%AA%E5%8F%AF%E4%BB%A5%E6%93%8D%E4%BD%9C%E5%A4%B4%E9%83%A8%EF%BC%89%0A%20%E9%98%9F%E5%88%97%E6%97%B6%E9%97%B4%E5%A4%8D%E6%9D%82%E5%BA%A6%EF%BC%9A%0A%20%20%20%20%E6%9F%A5%E6%89%BE%EF%BC%9AO(n)%0A%20%20%20%20%E6%8F%92%E5%85%A5%EF%BC%9AO(1)%20%E5%8D%B3%E5%85%A5%E9%98%9F%0A%20%20%20%20%E5%88%A0%E9%99%A4%EF%BC%9AO(1)%20%E5%8D%B3%E5%87%BA%E9%98%9F%0A%0A%0A%E6%BA%90%E7%A0%81%E5%88%86%E6%9E%90%EF%BC%9A%0A%60%60%60%0Atemplate%20%3Cclass%20Elem%3E%0Aclass%20Queue%0A%7B%0A%20%20public%3A%0A%20%20%20%20Queue(int%20MaxSize%3D500)%3B%0A%20%20%20%20Queue(const%20Queue%3CElem%3E%20%26OtherQueue)%3B%0A%20%20%20%20~Queue(void)%3B%0A%20%0A%20%20%20%20void%20%20%20%20%20%20%20Enqueue(const%20Elem%20%26Item)%3B%20%20%20%20%2F%2F%20Adds%20Item%20to%20Queue%20end%0A%20%20%20%20Elem%20%20%20%20%20%20%20Dequeue(void)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Returns%20Item%20from%20Queue%0A%20%20%20%20inline%20int%20ElemNum(void)%3B%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Returns%20Number%20of%20Elements%0A%20%0A%20%20protected%3A%0A%20%20%20%20Elem%20%20%20%20%20*Data%3B%20%20%20%20%20%20%2F%2F%20The%20actual%20Data%20array%0A%20%20%20%20const%20int%20MAX_NUM%3B%20%20%20%2F%2F%20The%20actual%20spaces%20will%20be%20one%20more%20than%20this%0A%20%20%20%20int%20%20%20%20%20%20%20Beginning%2C%20%2F%2F%20Numbered%20location%20of%20the%20start%20and%20end%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20End%3B%0A%20%0A%20%20%20%20%2F%2F%20Instead%20of%20calculating%20the%20number%20of%20elements%2C%20using%20this%20variable%0A%20%20%20%20%2F%2F%20is%20much%20more%20convenient.%0A%20%20%20%20int%20%20%20%20%20%20%20ElemCount%3B%0A%7D%3B%0A%60%60%60%0A%0A%0A%0A%0A