楼主: Lisrelchen
1699 6

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

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

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

楼主
Lisrelchen 发表于 2017-1-9 06:02:36 |AI写论文

+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

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

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2026-1-1 06:04