楼主: Lisrelchen
1392 6

C++ Templates: The Complete Guide [推广有奖]

  • 0关注
  • 62粉丝

VIP

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
49957 个
通用积分
79.5487
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

本帖隐藏的内容

C ++Templates.rar (725.2 KB, 需要: 5 个论坛币) 本附件包括:
  • C ++Templates.chm


  1. Publisher:        Addison-Wesley Professional
  2. Categories:        Addison Wesley , The Complete Guide
  3. Author:        David Vandevoorde
  4. Edition:        1
  5. ISBN-10:        0201734842
  6. ISBN-13:        9780201734843
  7. Pages:        552
  8. Published:        Nov 22 2002
  9. Posted:        Jun 30 2016
  10. Language:        English
  11. Book format:        CHM
  12. Book size:        0.72 MB
复制代码



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Templates Complete Template Guide plate

沙发
Lisrelchen 发表于 2017-1-9 06:04:43 |只看作者 |坛友微信交流群
  1. 3.1.1 Declaration of Class Templates
  2. Declaring class templates is similar to declaring function templates: Before the declaration, a statement declares an identifier as a type parameter. Again, T is usually used as an identifier:

  3. template <typename T>
  4. class Stack {
  5.   …
  6. };
  7. Here again, the keyword class can be used instead of typename:

  8. template <class T>
  9. class Stack {
  10.   …
  11. };
  12. Inside the class template, T can be used just like any other type to declare members and member functions. In this example, T is used to declare the type of the elements as vector of Ts, to declare push() as a member function that gets a constant T reference as an argument, and to declare top() as a function that returns a T:

  13. template <typename T>
  14. class Stack {
  15.   private:
  16.     std::vector<T> elems;  // elements

  17.   public:
  18.     Stack();               // constructor
  19.     void push(T const&);   // push element
  20.     void pop();            // pop element
  21.     T top() const;         // return top element
  22. };
复制代码

使用道具

藤椅
Lisrelchen 发表于 2017-1-9 06:05:34 |只看作者 |坛友微信交流群
  1. 3.1.2 Implementation of Member Functions
  2. To define a member function of a class template, you have to specify that it is a function template, and you have to use the full type qualification of the class template. Thus, the implementation of the member function push() for type Stack<T> looks like this:

  3. template <typename T>
  4. void Stack<T>::push (T const& elem)
  5. {
  6.     elems.push_back(elem);    // append copy of passed elem
  7. }
  8. In this case, push_back() of the element vector is called, which appends the element at the end of the vector.

  9. Note that pop_back() of a vector removes the last element but doesn't return it. The reason for this behavior is exception safety. It is impossible to implement a completely exception-safe version of pop() that returns the removed element (this topic was first discussed by Tom Cargill in [CargillExceptionSafety] and is discussed as Item 10 in [SutterExceptional]). However, ignoring this danger, we could implement a pop() that returns the element just removed. To do this, we simply use T to declare a local variable of the element type:

  10. template<typename T>
  11. T Stack<T>::pop ()
  12. {
  13.     if (elems.empty()) {
  14.         throw std::out_of_range("Stack<>::pop(): empty stack");
  15.     }
  16.     T elem = elems.back();    // save copy of last element
  17.     elems.pop_back();         // remove last element
  18.     return elem;              // return copy of saved element
  19. }
复制代码

使用道具

板凳
tcca6675 发表于 2017-1-9 09:18:12 |只看作者 |坛友微信交流群
看看。。。谢谢分享!!!

使用道具

报纸
ekscheng 发表于 2017-1-9 09:23:08 |只看作者 |坛友微信交流群

使用道具

地板
soccy 发表于 2017-1-9 09:41:58 |只看作者 |坛友微信交流群
......

使用道具

7
pensifia 发表于 2017-1-10 10:37:57 |只看作者 |坛友微信交流群

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-26 14:58